Limit the queryset of entries displayed for a django admin Inline

前端 未结 2 1944
鱼传尺愫
鱼传尺愫 2020-12-10 04:21

In django admin, using django 1.2, i\'m trying to add a InlineModelAdmin to apply a comment on save when a change is made to an entry. (An entry is expected to have a \"Chan

2条回答
  •  醉梦人生
    2020-12-10 04:46

    I am supposing you are using a

    models.ForeignKey(EntryAdmin)
    

    in your ChangeComment model. but if you want only one comment for each EntryAdmin, your should use instead a:

    models.OneToOneField(EntryAdmin)
    

    And you won't need your NoCommentsInlineFormset nor your inline class. That's what I would do.

    EDITED

    Ok then if you want to keep a history of comments, you could override the queryset in the NoCommentsInlineFormset as:

    def __init__(self, *args, **kwargs):
        super(NoCommentsInlineFormset, self).__init__(*args, **kwargs)
        self.queryset = ChangeComment.objects.order_by('-created_at')[:1]
    

    This should work.

提交回复
热议问题