user5137551
user5137551

Reputation: 23

Django - Adding a foreign key representing a user to a model

Couldn't seem to find an answer to my question anywhere. You're my only hope. So I have an existing model that has already been migrated. I want to add a foreign key to this model linking it to the 'auth.User' table. This is what my code looks like:

from django.db import models
from django.contrib.auth.models import User

class Beer(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    brand = models.CharField(max_length = 100,default='')
    beer_type = models.CharField(max_length = 100,default='')
    ml = models.IntegerField(default=330)
    owner = models.OneToOneField(User,default=User.objects.get(pk=2), related_name="beers")

I then try creating the migration file:

python manage.py makemigrations

And I get the following error:

Migrations for 'beers':
0003_beer_owner.py:
- Add field owner to beer
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management     /__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 444, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/makemigrations.py", line 143, in handle
self.write_migration_files(changes)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/makemigrations.py", line 171, in write_migration_files
migration_string = writer.as_string()
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/writer.py", line 166, in as_string
operation_string, operation_imports = OperationWriter(operation).serialize()
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/writer.py", line 124, in serialize
_write(arg_name, arg_value)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/writer.py", line 87, in _write
arg_string, arg_imports = MigrationWriter.serialize(_arg_value)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/writer.py", line 377, in serialize
return cls.serialize_deconstructed(path, args, kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/writer.py", line 268, in serialize_deconstructed
arg_string, arg_imports = cls.serialize(arg)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/writer.py", line 465, in serialize
"topics/migrations/#migration-serializing" % (value, get_docs_version())
ValueError: Cannot serialize: <User: user>
There are some values Django cannot serialize into migration files.
For more, see https://docs.djangoproject.com/en/1.8/topics/migrations    /#migration-serializing

Any have a clue how to fix it? It may have to do with what I am using as my default value. Apparently the user object can't be serialized. Thanks

Upvotes: 2

Views: 358

Answers (1)

Burhan Khalid
Burhan Khalid

Reputation: 174624

You need to provide the database equivalent value, and since you are linking it to a primary key, you need to provide the primary key as the default:

owner = models.OneToOneField(User,
                             default=User.objects.get(pk=2).pk,
                             related_name="beers")

Or, you can simplify the entire thing and keep it by providing the integer value directly:

owner = models.OneToOneField(User, default=2, related_name="beers")

Goes without saying; you should make sure you have User with that id in the database; ideally using data migrations.

Upvotes: 3

Related Questions