FieldDoesNotExist加载Fixture

时间:2015-09-22 19:44:22

标签: django django-fixtures

我试图加载名为initial_data.json的夹具:

[{"pk": 1, "model": "contenttypes.contenttype", "fields": {"model": "logentry", "name": "log entry", "app_label": "admin"}}, {"pk": 2, "model": "contenttypes.contenttype", "fields": {"model": "permission", "name": "permission", "app_label": "auth"}}, {"pk": 3, "model": "contenttypes.contenttype", "fields": {"model": "group", "name": "group", "app_label": "auth"}}, {"pk": 4, "model": "contenttypes.contenttype", "fields": {"model": "user", "name": "user", "app_label": "auth"}}, {"pk": 5, "model": "contenttypes.contenttype", "fields": {"model": "contenttype", "name": "content type", "app_label": "contenttypes"}}, {"pk": 6, "model": "contenttypes.contenttype", "fields": {"model": "session", "name": "session", "app_label": "sessions"}}, {"pk": 7, "model": "contenttypes.contenttype", "fields": {"model": "tweet", "name": "tweet", "app_label": "tweeter"}}, {"pk": "2pwa7lb5legwb02jw2h76f1e70j2jmo7", "model": "sessions.session", "fields": {"expire_date": "2014-09-14T00:20:41.773Z", "session_data": "OWQ4MmE3ZGRkZjliOWY3OTQ5OTdiMDIyYjE5ZWMwNmUwZTAyMThhMjp7Il9hdXRoX3VzZXJfYmFja2VuZCI6ImRqYW5nby5jb250cmliLmF1dGguYmFja2VuZHMuTW9kZWxCYWNrZW5kIiwiX2F1dGhfdXNlcl9pZCI6MX0="}}, {"pk": "ncl522f8kc6bf242chahmc2squtioirb", "model": "sessions.session", "fields": {"expire_date": "2014-09-14T01:19:42.407Z", "session_data": "NjY0NjliYjNkNDk0ODJhYWRjMDk5MjNjODA1NTAxYWIzZjg3ZGFkYzp7fQ=="}}, {"pk": 1, "model": "auth.permission", "fields": {"codename": "add_logentry", "name": "Can add log entry", "content_type": 1}}, {"pk": 2, "model": "auth.permission", "fields": {"codename": "change_logentry", "name": "Can change log entry", "content_type": 1}}, {"pk": 3, "model": "auth.permission", "fields": {"codename": "delete_logentry", "name": "Can delete log entry", "content_type": 1}}, {"pk": 4, "model": "auth.permission", "fields": {"codename": "add_permission", "name": "Can add permission", "content_type": 2}}, {"pk": 5, "model": "auth.permission", "fields": {"codename": "change_permission", "name": "Can change permission", "content_type": 2}}, {"pk": 6, "model": "auth.permission", "fields": {"codename": "delete_permission", "name": "Can delete permission", "content_type": 2}}, {"pk": 7, "model": "auth.permission", "fields": {"codename": "add_group", "name": "Can add group", "content_type": 3}}, {"pk": 8, "model": "auth.permission", "fields": {"codename": "change_group", "name": "Can change group", "content_type": 3}}, {"pk": 9, "model": "auth.permission", "fields": {"codename": "delete_group", "name": "Can delete group", "content_type": 3}}, {"pk": 10, "model": "auth.permission", "fields": {"codename": "add_user", "name": "Can add user", "content_type": 4}}, {"pk": 11, "model": "auth.permission", "fields": {"codename": "change_user", "name": "Can change user", "content_type": 4}}, {"pk": 12, "model": "auth.permission", "fields": {"codename": "delete_user", "name": "Can delete user", "content_type": 4}}, {"pk": 13, "model": "auth.permission", "fields": {"codename": "add_contenttype", "name": "Can add content type", "content_type": 5}}, {"pk": 14, "model": "auth.permission", "fields": {"codename": "change_contenttype", "name": "Can change content type", "content_type": 5}}, {"pk": 15, "model": "auth.permission", "fields": {"codename": "delete_contenttype", "name": "Can delete content type", "content_type": 5}}, {"pk": 16, "model": "auth.permission", "fields": {"codename": "add_session", "name": "Can add session", "content_type": 6}}, {"pk": 17, "model": "auth.permission", "fields": {"codename": "change_session", "name": "Can change session", "content_type": 6}}, {"pk": 18, "model": "auth.permission", "fields": {"codename": "delete_session", "name": "Can delete session", "content_type": 6}}, {"pk": 19, "model": "auth.permission", "fields": {"codename": "add_tweet", "name": "Can add tweet", "content_type": 7}}, {"pk": 20, "model": "auth.permission", "fields": {"codename": "change_tweet", "name": "Can change tweet", "content_type": 7}}, {"pk": 21, "model": "auth.permission", "fields": {"codename": "delete_tweet", "name": "Can delete tweet", "content_type": 7}}, {"pk": 1, "model": "auth.user", "fields": {"username": "nina", "first_name": "", "last_name": "", "is_active": true, "is_superuser": true, "is_staff": true, "last_login": "2014-08-31T00:20:41.771Z", "groups": [], "user_permissions": [], "password": "pbkdf2_sha256$12000$2LSBfxYO9fJJ$UT/BLyRLwBQIOUtOfA2aKkGw+xe44ZNYD2TWXqXoT3E=", "email": "", "date_joined": "2014-08-30T18:10:53.539Z"}}, {"pk": 2, "model": "auth.user", "fields": {"username": "admin", "first_name": "Admin", "last_name": "", "is_active": true, "is_superuser": true, "is_staff": true, "last_login": "2014-08-31T00:22:38Z", "groups": [], "user_permissions": [], "password": "pbkdf2_sha256$12000$6JY3bvlplRf0$Rm6rcK9M3LNuTw1uZ3B/Je7rq420UCaf2iwmY8pIv2U=", "email": "admin@admin.com", "date_joined": "2014-08-31T00:22:38Z"}}, {"pk": 3, "model": "auth.user", "fields": {"username": "bob", "first_name": "Bob", "last_name": "Bobman", "is_active": true, "is_superuser": false, "is_staff": false, "last_login": "2014-08-31T00:23:06Z", "groups": [], "user_permissions": [], "password": "pbkdf2_sha256$12000$ehuPT7BoKVpF$VZTxDeaHtLG7jU4wQ1erlFciwMk7l8aCp9MIjSVa/NU=", "email": "bob@bob.com", "date_joined": "2014-08-31T00:23:06Z"}}, {"pk": 4, "model": "auth.user", "fields": {"username": "amy", "first_name": "Amy", "last_name": "Smith", "is_active": true, "is_superuser": false, "is_staff": false, "last_login": "2014-08-31T00:23:24Z", "groups": [], "user_permissions": [], "password": "pbkdf2_sha256$12000$Tv4vWeWICkfS$qcMP+xddceQZMVjHbdOhOsV6LKOQntKOkiaqdEzS2p8=", "email": "amy@smith.com", "date_joined": "2014-08-31T00:23:24Z"}}, {"pk": 1, "model": "tweeter.tweet", "fields": {"text": "I'm an Admin! ", "user": 2, "timestamp": "2014-08-30T18:51:04Z"}}, {"pk": 2, "model": "tweeter.tweet", "fields": {"text": "Bob is the coolest name EVAR", "user": 3, "timestamp": "2014-08-29T18:51:19Z"}}, {"pk": 3, "model": "tweeter.tweet", "fields": {"text": "I <3 Tweeter", "user": 4, "timestamp": "2014-08-30T15:52:09Z"}}, {"pk": 1, "model": "admin.logentry", "fields": {"action_flag": 1, "action_time": "2014-08-31T00:22:38.417Z", "object_repr": "Admin", "object_id": "2", "change_message": "", "user": 1, "content_type": 4}}, {"pk": 2, "model": "admin.logentry", "fields": {"action_flag": 2, "action_time": "2014-08-31T00:22:57.048Z", "object_repr": "Admin", "object_id": "2", "change_message": "Changed first_name, email, is_staff and is_superuser.", "user": 1, "content_type": 4}}, {"pk": 3, "model": "admin.logentry", "fields": {"action_flag": 1, "action_time": "2014-08-31T00:23:06.858Z", "object_repr": "Bob", "object_id": "3", "change_message": "", "user": 1, "content_type": 4}}, {"pk": 4, "model": "admin.logentry", "fields": {"action_flag": 2, "action_time": "2014-08-31T00:23:17.098Z", "object_repr": "Bob", "object_id": "3", "change_message": "Changed first_name, last_name and email.", "user": 1, "content_type": 4}}, {"pk": 5, "model": "admin.logentry", "fields": {"action_flag": 1, "action_time": "2014-08-31T00:23:24.481Z", "object_repr": "Amy", "object_id": "4", "change_message": "", "user": 1, "content_type": 4}}, {"pk": 6, "model": "admin.logentry", "fields": {"action_flag": 2, "action_time": "2014-08-31T00:23:35.860Z", "object_repr": "Amy", "object_id": "4", "change_message": "Changed first_name, last_name and email.", "user": 1, "content_type": 4}}, {"pk": 7, "model": "admin.logentry", "fields": {"action_flag": 1, "action_time": "2014-08-31T00:51:05.451Z", "object_repr": "I'm an Admin! ", "object_id": "1", "change_message": "", "user": 1, "content_type": 7}}, {"pk": 8, "model": "admin.logentry", "fields": {"action_flag": 1, "action_time": "2014-08-31T00:51:26.097Z", "object_repr": "Bob is the coolest name EVAR", "object_id": "2", "change_message": "", "user": 1, "content_type": 7}}, {"pk": 9, "model": "admin.logentry", "fields": {"action_flag": 1, "action_time": "2014-08-31T00:52:12.916Z", "object_repr": "I <3 Tweeter", "object_id": "3", "change_message": "", "user": 1, "content_type": 7}}, {"pk": 10, "model": "admin.logentry", "fields": {"action_flag": 2, "action_time": "2014-08-31T01:06:18.846Z", "object_repr": "admin", "object_id": "2", "change_message": "Changed username.", "user": 1, "content_type": 4}}, {"pk": 11, "model": "admin.logentry", "fields": {"action_flag": 2, "action_time": "2014-08-31T01:06:26.076Z", "object_repr": "bob", "object_id": "3", "change_message": "Changed username.", "user": 1, "content_type": 4}}, {"pk": 12, "model": "admin.logentry", "fields": {"action_flag": 2, "action_time": "2014-08-31T01:06:34.096Z", "object_repr": "amy", "object_id": "4", "change_message": "Changed username.", "user": 1, "content_type": 4}}]

但是当我正在运行syncdb或使用load data时,我收到以下错误:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 393, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 444, in execute
    output = self.handle(*args, **options)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 25, in handle
    call_command("migrate", **options)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 120, in call_command
    return command.execute(*args, **defaults)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 444, in execute
    output = self.handle(*args, **options)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 179, in handle
    created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 364, in sync_apps
    hide_empty=True,
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 120, in call_command
    return command.execute(*args, **defaults)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 444, in execute
    output = self.handle(*args, **options)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 60, in handle
    self.loaddata(fixture_labels)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 90, in loaddata
    self.load_label(fixture_label)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 141, in load_label
    for obj in objects:
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/serializers/json.py", line 84, in Deserializer
    six.reraise(DeserializationError, DeserializationError(e), sys.exc_info()[2])
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/serializers/json.py", line 78, in Deserializer
    for obj in PythonDeserializer(objects, **options):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/serializers/python.py", line 120, in Deserializer
    field = Model._meta.get_field(field_name)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/options.py", line 554, in get_field
    raise FieldDoesNotExist('%s has no field named %r' % (self.object_name, field_name))
django.core.serializers.base.DeserializationError: Problem installing fixture '/Users/wrb473/Documents/workspace/angulardjango/tweeter/fixtures/initial_data.json': ContentType has no field named u'name'

灯具的问题在哪里?

2 个答案:

答案 0 :(得分:2)

在此页面上:https://docs.djangoproject.com/en/1.8/ref/contrib/contenttypes/#django.contrib.contenttypes.models.ContentType

&#34;在Django 1.8中更改: 在Django 1.8之前,name属性是ContentType模型上的一个真实字段。&#34; - 暗示从1.8开始,它不是一个真实的领域。你使用的是1.8吗?如果从ContentType夹具中删除名称,它是否有效?

答案 1 :(得分:1)

这是因为ContentType.name是属性,而不是Field

摘自ContentType模型定义:

class ContentType(models.Model):
    app_label = models.CharField(max_length=100)
    model = models.CharField(_('python model class name'), max_length=100)
    objects = ContentTypeManager()

    # ...

    @property
    def name(self):
    # ...

此模型上定义的唯一字段是app_labelmodel。 这意味着序列化对象的代码也包含属性,在反序列化时无法设置其值。

您可以尝试从序列化数据中删除任何非模型Field(在您的情况下仅为name)。