没有外键的Django Admin Inline

时间:2018-06-04 08:59:49

标签: python django

我看到很多问题和答案,但我找不到一定的答案。

Admin inline without foreign key

Django admin inline display but foreign key has opposite relation

我可以迭代模型字段而不与其他模型相关

class CommentTabularInline(admin.TabularInline):
    model = Comment
    fields = ('content_type', 'text')
    extra = 1

class CommentAdmin(admin.ModelAdmin):
    inlines = [CommentTabularInline]

admin.site.register(Comment, CommentAdmin)

模型

class Comment(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, CASCADE, 'user_comment')
    text = models.TextField()
    content_type = models.ForeignKey(ContentType, CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')
    created_at = models.DateTimeField(auto_now=True)
    is_approved = models.BooleanField(default=False)

我试图这样做,但我收到错误

  

:( admin.E202)   'main.Comment'对'main.Comment'没有ForeignKey

0 个答案:

没有答案