invalid literal for int() with base 10: 'pk' Python/Django -
i'm working on project in django , added field 1 of models saves user created it. set default value of 'none'. now, whenever try migrate value error saying "invalid literal int() base 10: 'pk'". here's model looks like:
class samplemodel(models.model): created_by = models.foreignkey(user, default=none) # other fields here
and here's traceback when try run python manage.py migrate
:
operations perform: synchronize unmigrated apps: staticfiles, messages apply migrations: admin, contenttypes, posts, auth, sessions synchronizing apps without migrations: creating tables... running deferred sql... installing custom sql... running migrations: rendering model states... done applying posts.0002_post_created_by...traceback (most recent call last): file "manage.py", line 10, in <module> execute_from_command_line(sys.argv) file "/library/python/2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line utility.execute() file "/library/python/2.7/site-packages/django/core/management/__init__.py", line 330, in execute self.fetch_command(subcommand).run_from_argv(self.argv) file "/library/python/2.7/site-packages/django/core/management/base.py", line 393, in run_from_argv self.execute(*args, **cmd_options) file "/library/python/2.7/site-packages/django/core/management/base.py", line 444, in execute output = self.handle(*args, **options) file "/library/python/2.7/site-packages/django/core/management/commands/migrate.py", line 221, in handle executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial) file "/library/python/2.7/site-packages/django/db/migrations/executor.py", line 110, in migrate self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial) file "/library/python/2.7/site-packages/django/db/migrations/executor.py", line 148, in apply_migration state = migration.apply(state, schema_editor) file "/library/python/2.7/site-packages/django/db/migrations/migration.py", line 115, in apply operation.database_forwards(self.app_label, schema_editor, old_state, project_state) file "/library/python/2.7/site-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards field, file "/library/python/2.7/site-packages/django/db/backends/sqlite3/schema.py", line 179, in add_field self._remake_table(model, create_fields=[field]) file "/library/python/2.7/site-packages/django/db/backends/sqlite3/schema.py", line 77, in _remake_table self.effective_default(field) file "/library/python/2.7/site-packages/django/db/backends/base/schema.py", line 211, in effective_default default = field.get_db_prep_save(default, self.connection) file "/library/python/2.7/site-packages/django/db/models/fields/related.py", line 1956, in get_db_prep_save return self.related_field.get_db_prep_save(value, connection=connection) file "/library/python/2.7/site-packages/django/db/models/fields/__init__.py", line 710, in get_db_prep_save prepared=false) file "/library/python/2.7/site-packages/django/db/models/fields/__init__.py", line 977, in get_db_prep_value value = self.get_prep_value(value) file "/library/python/2.7/site-packages/django/db/models/fields/__init__.py", line 985, in get_prep_value return int(value) valueerror: invalid literal int() base 10: 'pk'
now figure has primary key of user
i'm not sure how fix since lines in traceback in actual python files. help.
edit: posts/migrations/0002_post_created_by.py added request:
# -*- coding: utf-8 -*- __future__ import unicode_literals django.db import models, migrations django.conf import settings class migration(migrations.migration): dependencies = [ migrations.swappable_dependency(settings.auth_user_model), ('posts', '0001_initial'), ] operations = [ migrations.addfield( model_name='post', name='created_by', field=models.foreignkey(default=b'pk', to=settings.auth_user_model), ), ]
it looks have set default b'pk'
before, made migration, changed default none
, tried run migration.
you need delete old (unapplied) migration , create new 1 when default=none
set.
be aware default=none
case anyways, , none
default isn't valid null=false
(which implicitly set here).
Comments
Post a Comment