gpt4 book ai didi

python - 只读现有字段但允许在 django admin 中创建新内联时添加

转载 作者:行者123 更新时间:2023-11-28 18:47:08 25 4
gpt4 key购买 nike

我有两个模型,其中一个与另一个内联。我已将内联模型的字段设置为只读。

class FollowUpInLine(admin.TabularInline):
model = md.FollowUp
extra = 0
can_delete = False

def get_readonly_fields(self, request, obj=None):
if request.user.is_superuser == False:
if obj: # editing an existing object
return self.readonly_fields + (
'follow_up_date',
'status_inquiry',
'remarks',
'followup_done_by',
)
return self.readonly_fields

但是,当在内联中单击“添加另一个”时,这不允许添加新字段,而是将它们更改为值为“无”的标签。如何使字段内联但在添加下一个内联时添加?

最佳答案

我找到了这个问题的答案。我们需要插入一个构造表单,然后像我所做的那样从内联类中调用此表单,如下所示:

class RequiredInlineFormSet(BaseInlineFormSet):
"""
Generates an inline formset that is required
"""
def _construct_form(self, i, **kwargs):
"""
Override the method to change the 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
extra = 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')
extra = 0
can_delete = False
formset = RequiredInlineFormSet

def has_add_permission(self, request):
return False

class FollowUpAdminInLine(admin.TabularInline):
model = md.FollowUp
extra = 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 the FollowUp to be added along with the 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 an 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 to those that belong to the 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)

关于python - 只读现有字段但允许在 django admin 中创建新内联时添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18269366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com