问题
I am a django beginner, and I am trying to make a child-parent like combo box, (bars depends on city depends on country) and I get this error.
UPDATE: Changed the model and the default value for the foreign key, but still the same error. Any help? thanks!
this is models.py:
from django.db import models
from smart_selects.db_fields import ChainedForeignKey
DEFAULT_COUNTRY_ID = 1 # id of Israel
class BarName(models.Model):
name = models.CharField(max_length=20)
def __unicode__(self):
return self.name
class Country(models.Model):
country = models.CharField(max_length=20)
def __unicode__(self):
return self.country
class City(models.Model):
city = models.CharField(max_length=20)
country = models.ForeignKey(Country, default=DEFAULT_COUNTRY_ID)
def __unicode__(self):
return self.city
class Bar(models.Model):
country = models.ForeignKey(Country)
city = ChainedForeignKey(City, chained_field='country' , chained_model_field='country', auto_choose=True)
name = ChainedForeignKey(BarName, chained_field='city', chained_model_field='city', auto_choose=True)
def __unicode__(self):
return '%s %s %s' % (self.name, self.city, self.country)
class Admin:
pass
from running 'python manage.py migrate':
➜ baromatix python manage.py migrate
Operations to perform:
Apply all migrations: admin, bars, contenttypes, auth, sessions
Running migrations:
.......
value = self.get_prep_value(value)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 915, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'DEFAULT VALUE'
回答1:
I solved this issue when deleting the old migrations files that django created.
回答2:
You should use id of Country
for foreignkey
default value:
DEFAULT_COUNTRY_ID = id # id of Israel
class City(models.Model):
city = models.CharField(max_length=20)
country = models.ForeignKey(Country, default=DEFAULT_COUNTRY_ID))
def __unicode__(self):
return self.city
回答3:
Elaborating Alon's Weissfeld answer:
I ran into the same problem and it was indeed fixed after I deleted all the migration cache files located here:
~\yourAppFolder\migrations\
Do not delete __init__.py
来源:https://stackoverflow.com/questions/27134290/invalid-literal-for-int-with-base-10-django-updated