1👍
✅
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)
👤AKS
0👍
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
- Simple Filter in django rest framework
- Local variable 'template' referenced before assignment
- Django ForeignKeys not saving
- Issue with Django URL Mapping/DetailView
Source:stackexchange.com