#23454: TypeError: int() argument must be a string or a number, not 'User'
-----------------------------+--------------------------------------
     Reporter:  acooley2006  |                    Owner:  nobody
         Type:  Bug          |                   Status:  new
    Component:  Migrations   |                  Version:  1.7
     Severity:  Normal       |               Resolution:
     Keywords:               |             Triage Stage:  Unreviewed
    Has patch:  0            |      Needs documentation:  0
  Needs tests:  0            |  Patch needs improvement:  0
Easy pickings:  1            |                    UI/UX:  0
-----------------------------+--------------------------------------
Changes (by fupduck):

 * status:  closed => new
 * resolution:  invalid =>


Comment:

 Hello,

 this error message comes up when a default user is assigned to a new
 foreign key field.
 Excerpt of the attached project where manage.py migrate is failing:

 {{{
 from django.db import models
 from django.contrib.auth.models import User
 def get_default_user():
     return User.objects.get(id=1)

 class Question(models.Model):
     the_user = models.ForeignKey(User, default=get_default_user)
 }}}

 Then manage.py makemigrations leads to a new migration 0003 with:

 {{{
 migrations.AddField(
             model_name='question',
             name='the_user',
             field=models.ForeignKey(default=polls.models.get_default_user,
 to=settings.AUTH_USER_MODEL),
         ),
 }}}
 And then manage.py sqlmigrate polls 0003 is enough to get the error
 message:
 {{{
   File "/usr/lib/python2.7/dist-
 packages/django/db/models/fields/__init__.py", line 985, in get_prep_value
     return int(value)
 TypeError: int() argument must be a string or a number, not 'User'
 }}}
 A workaround is directly assining the .id of the wanted user to the
 default parameter, but I think it would be better if int(user_instance)
 would either return the id as int or directly user_instance.id is used by
 get_prep_value.

 Regards

 Link to example project to reproduce the code (tar.gz):
 [https://slack-files.com/T031HCE60-F0FBKUE87-a1a23a53f7]

--
Ticket URL: <https://code.djangoproject.com/ticket/23454#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/069.0894f40d80015d9671a6caf965b62304%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to