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

时间:2013-08-16 08:49:23

标签: python django django-admin

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

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

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

1 个答案:

答案 0 :(得分:1)

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

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)