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'
I solved this issue when deleting the old migrations files that django created.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With