Django:通用外键转储数据:无法解析依赖项

问题描述 投票:5回答:2

我使用通用外键将不同的配置文件与从Users继承的我的auth.User模型相关联。尽管通过了dumpdata选项,但我无法执行--natural。它说,

错误:无法解析myproject.AdminProfile,myproject.TeacherProfile,myproject.users(已序列化应用列表中的用户)。]

documentation说,我们需要实现natural_key methods来拍摄和涉及通用关系的固定装置。我该如何使用这里展示的模型来做到这一点?

class Users(User):
    location = models.TextField('Location', blank=True)
    created_by = models.ForeignKey('self', null=True, blank=True, related_name='created_by_user')

    # Generic foreign key setup to hold the extra attributes
    profile_contenttype = models.ForeignKey(ContentType, null=True, blank=True)
    profile_object_id = models.PositiveIntegerField('Extra ID', null=True, blank=True)
    profile_object = generic.GenericForeignKey('profile_contenttype', 'profile_object_id')


class AdminProfile(models.Model):
    organization = models.CharField('Organization', max_length=100)

    # profile reverse relation to get the user
    users_link = generic.GenericRelation('Users', content_type_field='profile_contenttype',
                                         object_id_field='profile_object_id')

class TeacherProfile(models.Model):
    designation = models.CharField('Designation', max_length=100)

    # profile reverse to get the user
    users_link = generic.GenericRelation('Users', content_type_field='profile_contenttype',
                                         object_id_field='profile_object_id')

使用Django 1.4.3和Postrgres。

我使用通用外键将不同的配置文件与从auth.User继承的我的用户模型相关联。尽管通过了--natural选项,但我无法执行dumpdata。它说,错误:...

django generics foreign-key-relationship natural-key dumpdata
2个回答
7
投票

您的问题似乎与缺少自然键方法无关。我使用SQLite在Django 1.4和1.2.5上按原样测试了[原始]代码,并且能够使用自然键正确地转储数据而没有错误。


0
投票

更普遍的问题(涉及自然键的循环依赖关系,而不仅仅是具有自然键的自引用)是一个开放的错误,请在此处报告:https://code.djangoproject.com/ticket/31051

© www.soinside.com 2019 - 2024. All rights reserved.