(admin.E301)'app.model'没有GenericForeignKey

时间:2016-02-10 07:47:42

标签: django django-models django-admin

在models.py

class Activities(models.Model):
    day = models.PositiveIntegerField(default=1, null=False,blank=False,db_index=True)
    activity = models.CharField(max_length=100, blank=False,choices=activity_choices, db_index=True)
    description = models.TextField(null=True,blank=True)
    sequence = models.PositiveIntegerField(null=True,blank=True)

class holidaygroup(models.Model):
    ....
    activity = generic.GenericRelation(Activities)

在admin.py

class ActivitiesInline(generic.GenericTabularInline):
    model = Activities
class holidaygroupAdmin(ReadOnlyAdminFields,admin.ModelAdmin):
    ....
    inlines = [SeasonalPriceInline,ImageInline,HotelsInline,seoInline,NotesInline,ActivitiesInline,]

当我试图运行服务器时出现以下错误

ERRORS:
<class 'holidays.admin.ActivitiesInline'>: (admin.E301) 'holidays.Activities' has no GenericForeignKey.

提前致谢

1 个答案:

答案 0 :(得分:1)

您当前的模型结构是每个活动下您有零个或多个假日组。如果您尝试将多个Activities存储在一个holidaygroup下,则应在Activities模型中使用外键:

class holidaygroup(models.Model):
    ...

class Activities(models.Model):
    ...
    holiday_group = models.ForeignKey(holidaygroup)

这样,每个Activities下都可以有一个holidaygroup内联列表。