Reputation: 228
I'm very new to Django and am primarily working through tutorials. I was attempting to create a model that has an author field. At a certain point when I was going through a migration process and used makemigrations, I received a series of messages in the console stating that I needed to set up default values for many of my fields in my models. So for author I put default='someauthor'
.
I later removed these defaults, still confused as to why that happened. I then went through the 'makemigrations' again and those messages were no longer prompting me. I forget what I did to change that.
However when I try to migrate 'python manage.py migrate' I get this error:
ValueError: invalid literal for int() with base 10: 'someauthor'
I've removed all references to "default='someauthor'" but this error remains. I will add that I have two models each of which make a reference to an author:
author = models.ForeignKey(User, related_name='tutorial_posts')
and
author = models.ForeignKey(User, related_name='blog_posts')
I feel like that has caused the problem. Any suggestions?
Upvotes: 0
Views: 355
Reputation: 1
I had the same issue and could not proceed with any other migrations
I was able to resolve by searching in the project directory for the string passed as the default value, in your case 'someauthor'
You should be able to find in the migrations folder a .py migration file.
`
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('accounts', '0004_auto_20191221_2233'),
]
operations = [
migrations.RemoveField(
model_name='useraccount',
name='username',
),
migrations.AddField(
model_name='useraccount',
name='user',
field=models.ForeignKey(default='string', on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
preserve_default=False,
),
]`
Change the default='string'
to 1
Upvotes: 0
Reputation: 19861
ForeignKey field stores the id from the other table which is by default an int
. If you add a default value as string
it won't be able to convert it to int and provide you this error:
ValueError: invalid literal for int() with base 10: 'someauthor'
You can check this yourself by trying this in a console:
In [3]: int('someauthor')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-3-3c4d496afcf9> in <module>()
----> 1 int('someauthor')
ValueError: invalid literal for int() with base 10: 'someauthor'
What I suggest is that either you use an int value for default or set null=True
:
author = models.ForeignKey(User, related_name='blog_posts', null=True)
Upvotes: 1