Wednesday, March 11, 2015

Django 1.7.6 TypeError when applying migration with ForeignKey with default

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?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/de0d382b-d21f-4c10-9fb6-d7f0e2791022%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment