Django 1.7.6 TypeError when applying migration with ForeignKey with default

82 views
Skip to first unread message

Krzysztof Ciebiera

unread,
Mar 11, 2015, 7:43:35 AM3/11/15
to django...@googlegroups.com
I have created a model A:

def g():
    return A.objects.get(pk=1)

class A(models.Model):
    a = models.IntegerField()

./manage.py makemigrations

./manage.py migrate

then I've added some data:

a = A(a=1)
a.save()

then I have created another model with a ForeignKey field with a default set to g function (that really returns one instance od A) and I was trying to migrate it:

class B(models.Model):
    b = models.ForeignKey(A, default=g)

./manage.py makemigrations
...
./manage.py migrate
....
  File ".../e/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 627, in get_db_prep_save
    prepared=False)
  File ".../e/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 907, in get_db_prep_value
    value = self.get_prep_value(value)
  File ".../p/e/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 915, in get_prep_value
    return int(value)
TypeError: int() argument must be a string or a number, not 'A'

Is it expected behaviour or a bug?

Collin Anderson

unread,
Mar 12, 2015, 2:36:51 PM3/12/15
to django...@googlegroups.com
Hi,

Maybe g() should return the id instead of the instance? That does seem a bit odd that it wouldn't accept an A() instance as the default.

Collin
Reply all
Reply to author
Forward
0 new messages