3

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?

Raj Subit
  • 1,487
  • 2
  • 12
  • 23
  • 1
    I too have the same question http://stackoverflow.com/questions/11574795/django-inline-allow-adding-disable-editing – arulmr Aug 16 '13 at 08:54
  • 1
    The `obj` that you are getting is the parent object. Its a bug in django - https://code.djangoproject.com/ticket/15602 – ssapkota Aug 18 '13 at 20:57
  • yeah and i also found that the add_permission in inline requires a change permission because it creates an intermediate model when add is clicked and without change permission the intermediate could not be changed so it was giving label while trying to add ... https://github.com/django/django/blob/master/django/contrib/admin/options.py – Raj Subit Aug 20 '13 at 08:22

1 Answers1

1

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)
Raj Subit
  • 1,487
  • 2
  • 12
  • 23