开发者

how to add readonly_field in django admin site?

I have the following problem:

I have two models: Article and Comment, in Comments, i have parent = models.ForeignKey(Article). I have it set up so that Comments is inline to ArticleAdmin(admin.ModelAdmin), and CommentInline(admin.StackedInline). Also, all fields in CommentInline i've put into readonly_fields. What I would like to do is that in the admin interface, there is a button on the bottom of Article that says "Add another Artcile", and after clicking that button, you can add new comment. However, i would like to have all the old comments to be read only (no one can edit开发者_如何学编程 it in the admin site). Therefore, i would like to have a append only readonly design. Right now i have the readonly_field setup, and when i click on "Add another Comment", it doesn't allow me to edit anything.

anyone have any suggestion on how to go about accomplishing this?

Thank you very much for your help!


you could override your inline admin's get_readonly_fields method:

    def get_readonly_fields(self, request, obj=None):
        if obj:
            return ('readonly_field1',......)
        else:
            return self.readonly_fields


How about doing a custom template that displays your comments and the rest of the form is then standard.
Here might be a good place to start: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#custom-template-options

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜