如何创建包含GenericRelation的灯具?

时间:2019-04-02 06:03:37

标签: django fixtures django-contenttypes dumpdata generic-relations

我正在尝试为我的测试用例创建一个夹具。但是,当我运行python manage.py test和python manage.py loaddata时,我总是收到错误消息:

TypeError:安装固定装置时出现问题...“ GenericRelatedObjectManager”对象不可迭代

在创建json文件时,我曾使用exclude删除内容类型,但仍然遇到此错误。

python manage.py dumpdata --exclude=contenttypes --exclude=auth --natural-foreign --natural-primary --format=json --indent=4 > website/fixtures/website.json

通过追溯,错误发生在:

文件“ C:\ Users \ Name \ Anaconda3 \ lib \ site-packages \ django \ contrib \ contenttypes \ fields.py”,位于第614行,

objs = tuple(objs)

它试图遍历具有一般关系的名为“收藏夹”的字段。数据存储了以下

'favorite':<django.contrib.contenttypes.fields.create_generic_related_manager.<locals>.GenericRelatedObjectManager object at 0x000001C758C25470>

这是我的模特:

class Activity(models.Model):
    FAVORITE = 'F'

    ACTIVITY_TYPES = (
        (FAVORITE, 'Favorite'),
    )
    user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
    activity_type = models.CharField(max_length=1, choices=ACTIVITY_TYPES)
    date = models.DateTimeField(auto_now_add=True)

    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey()

    def __str__(self):
        return self.activity_type 


class Page(models.Model):
    #need to see how to use pk of user already signed in
    user = models.ForeignKey(CustomUser , on_delete=models.CASCADE)
    name = models.CharField(max_length=100)
    description = models.TextField(blank=False, null=False)
    favorite = GenericRelation(Activity, related_query_name='favorite')
    ratings = GenericRelation(Rating, related_query_name='object_list')

    def __str__(self):
        return  self.recipeName

    def description_list(self):
        return self.description.split('\r\n')

我使用Django 1.10.5

0 个答案:

没有答案