Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

invalid literal for int() with base 10 - django - updated

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'
like image 904
Alon Weissfeld Avatar asked Dec 25 '22 01:12

Alon Weissfeld


2 Answers

I solved this issue when deleting the old migrations files that django created.

like image 130
Alon Weissfeld Avatar answered Dec 28 '22 05:12

Alon Weissfeld


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
like image 45
Hasan Ramezani Avatar answered Dec 28 '22 07:12

Hasan Ramezani