如何在django admin中显示(分层)对象成员

时间:2018-08-29 17:26:13

标签: python django django-admin django-comments

我正在将django 2.0.8与Python 3.5和django-threadedcomments结合使用以进行评论。

我正在尝试实现一个支持某些“社交”功能的博客(是的,我知道django博客应用程序已经存在!)

  1. 喜欢博客帖子(或任何其他模型)的能力
  2. 报告博客帖子(或任何其他模型)的能力
  3. 评论帖子的能力
  4. 能够拇指向上/向下滑动并报告评论

如前所述,我正在使用django-threadedcomments进行评论。

以下是我的一些型号:

foo / models.py

from django.db import models
from django.contrib.contenttypes.fields import GenericRelation
from threadedcomments.models import ThreadedComment
from django.conf import settings

class AbuseReport(models.Model):
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, blank=False, null=False, default=1, on_delete = models.CASCADE)   


class Reportable(models.Model):
    abuses = GenericRelation(AbuseReport)

    class Meta:
        abstract = True


class Like(models.Model):
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, blank=False, null=False, default=1, on_delete = models.CASCADE)


class Likeable(models.Model):
    likes = GenericRelation(Like)

    class Meta:
        abstract = True

class ThumbVote(models.Model):
    VOTE_TYPES = (
        ('U', 'Upvote'),
        ('D', 'Downvote'),
    )

    owner = models.ForeignKey(settings.AUTH_USER_MODEL, blank=False, null=False, default=1, on_delete = models.CASCADE)    
    vote  = models.CharField(max_length=1, choices=VOTE_TYPES)



class Thumbvoteable(models.Model):
    thumbvotes = GenericRelation(ThumbVote)

    class Meta:
        abstract = True


class ThumbvoteableReportableComment(Thumbvoteable, ThreadedComment):
    pass

foobar / models.py

from foo.models import ThumbvoteableReportableComment \
                       Likeable, Reportable

# Create your models here.
class Post(Likeable, Reportable):
    pass

class PostComments(ThumbvoteableReportableComment):
    post = models.ForeignKey(Post, related_name='comments', on_delete = models.CASCADE)

foobar / admin.py

from django.contrib import admin
from .models import Post, PostComments

class PostCommentsInline(admin.TabularInline):
    model = PostComments     
    readonly_fields = ('thumbvotes',)
    fields = ('comment', 'abuse_report_count')  


class PostAdmin(admin.ModelAdmin):
    prepopulated_fields = {"slug": ("title",)}
    list_display = ('title', 'pub_date')

    inlines = [PostFileInline, PostPictureGalleryInline, PostCommentsInline]

    def save_model(self, request, obj, form, change):
        obj.author = request.user
        obj.save()

# Register your models here.
admin.site.register(Post, PostAdmin)

当我进入django管理页面时,出现以下问题:

  1. Post对象没有abuses字段
  2. Post对象没有likes字段
  3. PostComment添加表单没有abuse_report_count字段,也没有thumbvote字段(均从父类继承)

如何更改模型等以使字段显示在管理器中?

0 个答案:

没有答案