Opened 10 years ago

Closed 9 years ago

#23454 closed Bug (invalid)

TypeError: int() argument must be a string or a number, not 'User'

Reported by: adam Owned by: nobody
Component: Migrations Version: 1.8
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Having a hard time with a change I made on a django site, I moved from a single app to multiple apps and now I'm getting a traceback when attempting to migrate an initial installation. Attaching the traceback here to see if anyone can help me out. i tested this again with a single app and large model and it works fine, only difference is in model relationships. (I think...:-)...)

Attachments (1)

trackback.txt (3.1 KB ) - added by adam 10 years ago.
traceback file

Download all attachments as: .zip

Change History (6)

by adam, 10 years ago

Attachment: trackback.txt added

traceback file

comment:1 by adam, 10 years ago

Easy pickings: set

comment:2 by Tim Graham, 10 years ago

Resolution: invalid
Status: newclosed

This ticket tracker is for bugs and new features suggestions. Please see TicketClosingReasons/UseSupportChannels for ways to get help. You'll likely need to provide more information in order to get help.

comment:3 by fupduck, 9 years ago

Resolution: invalid
Status: closednew

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

comment:4 by fupduck, 9 years ago

Version: 1.71.8

comment:5 by Tim Graham, 9 years ago

Resolution: invalid
Status: newclosed

The requirement to use the primary key is documented in the documentation and the decision discussed in #25129:

For fields like ForeignKey that map to model instances, defaults should be the value of the field they reference (pk unless to_field is set) instead of model instances.

Note: See TracTickets for help on using tickets.
Back to Top