为现有字段设置只读,但允许在 django admin 中创建新的内联时添加

Making readonly for existing field but allowing to add when new inline is created in django admin(为现有字段设置只读,但允许在 django admin 中创建新的内联时添加)

本文介绍了为现有字段设置只读,但允许在 django admin 中创建新的内联时添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two models with one of them as inline to other. I have made fields of inline model read only.

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

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

However this does not allows to add new fields when "Add another" is clicked in inline rather it changes them to label with value "None". How can I make fields inline but add when next inline is to added?

推荐答案

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

I found the answer to this. We need to insert a construct form and then call this form from the inline class as I have done which is shown below:

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)

这篇关于为现有字段设置只读,但允许在 django admin 中创建新的内联时添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为现有字段设置只读,但允许在 django admin 中创建新的内联时添加

基础教程推荐