python - Making readonly for existing field but allowing to add when new inline is created in django admin -
i have 2 models 1 of them inline other. have made fields of inline model read only.
class followupinline(admin.tabularinline): model = md.followup = 0 can_delete = false def get_readonly_fields(self, request, obj=none): if request.user.is_superuser == false: if obj: # editing existing object return self.readonly_fields + ( 'follow_up_date', 'status_inquiry', 'remarks', 'followup_done_by', ) return self.readonly_fields
however not allows add new fields when "add another" clicked in inline rather changes them label value "none". how can make fields inline add when next inline added?
i found answer this. need insert construct form , call form inline class have done shown below:
class requiredinlineformset(baseinlineformset): """ generates inline formset required """ def _construct_form(self, i, **kwargs): """ override method change form attribute empty_permitted """ form = super(requiredinlineformset, self)._construct_form(i, **kwargs) form.empty_permitted = false return form class followupaddinline(admin.tabularinline): model = md.followup = 1 formfield_overrides = { models.charfield: {'widget': textinput(attrs={'size':'20'})}, models.textfield: {'widget': textarea(attrs={'rows':4, 'cols':40})}, } can_delete = false formset = requiredinlineformset def has_change_permission(self, request, obj=none): return false class followuplistinline(admin.tabularinline): model = md.followup readonly_fields = ('status', 'follow_up_date', 'followup_status', 'followup_reason', 'remarks', 'followup_done_by') = 0 can_delete = false formset = requiredinlineformset def has_add_permission(self, request): return false class followupadmininline(admin.tabularinline): model = md.followup = 1 formfield_overrides = { models.charfield: {'widget': textinput(attrs={'size':'20'})}, models.textfield: {'widget': textarea(attrs={'rows':4, 'cols':40})}, } formset = requiredinlineformset def queryset(self, request): return md.followup.objects.filter(owner=request.user) class packageadmin(admin.modeladmin): """makes followup added along package""" inlines =(followuplistinline, followupaddinline) fields = ('date_of_inquiry', 'agent_name', 'type_of_booking', 'no_of_pax', 'source_of_inquiry', 'business_vendor', 'travel_date', 'reply_date', 'client_name', 'client_email', 'client_contacts', 'inquiry_assigned_to', 'inquiry_assigned_by') list_display = ('agent_name', 'date_of_inquiry','status_color') list_filter = ('date_of_inquiry',) can_delete = false list_per_page = 25 def get_readonly_fields(self, request, obj=none): if request.user.is_superuser == false: if obj: # editing existing object return self.readonly_fields + ( 'agent_name', 'date_of_inquiry', ) else: self.inlines = (followupadmininline,) return self.readonly_fields def queryset(self, request): """limit pages belong request's user.""" qs = super(packageadmin, self).queryset(request) if request.user.is_superuser: return qs return qs.filter(inquiry_assigned_to=request.user) admin.site.register(md.package,packageadmin)
Comments
Post a Comment