开发者

How can I add an option to a field inherited from a parent class?

How can I add in my custom class an option to a field existing in the parent model? More concretely: I'm writing a custom comment model inheriting from django.contrib.comments.models.Comment. I'd like to add t开发者_开发百科he option editable = False to the IPAddressField.

thank you


I don't know of a way to add an option to an existing field in a super class (if anyone knows better do share). You cannot override the field either as the superclass is not abstract.

If you only want to prevent the field from getting edited you can use a custom model form. This form can validate to make sure that the IP Address field cannot be edited.


I'm assuming you don't want to display or make it editable in the Admin.

from django.contrib import admin
from django.contrib.comments.models import Comment

class CommentAdmin(admin.ModelAdmin):
    exclude = ('ip_address',)

admin.site.unregister(Comment)
admin.site.unregister(Comment, CommentAdmin)

Alternatively, you can use ready_only:

readonly_fields = ['ip_address']
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜