17

I have two models, a MainModel and a related InlineModel that i'd like to show as an inline in the admin. This InlineModel can be used for, say, making notes about the model and should track the logged in admin user making changes. While this seems simple (and indeed, the docs show an example for this when the user field is part of the MainModel), I can't seem to grasp it when the field is on the Inline.

To be specific, my goal is:

  1. User edits MainModel
  2. User adds an InlineModel, not filling in the user field
  3. User presses save
  4. Code fills in the user field for newly created InlineModel instances
  5. (Bonus! user field is readonly for existing instances and hidden for new inlines)

And my questions:

  1. Is this correct? Its too bas save_model isn't called for InlineModelAdmin instances
  2. Does doing it this way allow me to save without causing an error? (user is required, validation flags it)
  3. How can I hide the user input field for new inlines, and have it readonly for existing inlines?

Here are my current ideas:


#models.py
class MainModel(models.Model):
    some_info = models.IntegerField()

class InlineModel(models.Model):
    main = models.ForeignKey(MainModel)
    data = models.CharField(max_length=255)
    user = models.ForeignKey('auth.User')

#admin.py
class InlineModelInline(admin.TabularInline):
    model = InlineModel
    fields = ('data', 'user')
    #readonly_fields = ('data', 'user') #Bonus question later

class MainModelAdmin(admin.ModelAdmin):
    list_display = ('id', 'some_info')
    inlines = [InlineModelInline]

    #def save_model(self, request, obj, form, change):
        #http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model
        #Only called for MainModel, not for any of the inlines
        #Otherwise, would be ideal

    def save_formset(self, request, form, formset, change):
        #http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_formset
        #Experimenting showd this is called once per formset (where the formset is a group of inlines)
        #See code block at http://code.djangoproject.com/browser/django/tags/releases/1.2.1/django/contrib/admin/options.py#L894
        if not isinstance(formset.model, InlineModel):
            return super(MainModelAdmin, self).save_formset(request, form, formset, change)
        instances = formset.save(commit=False)
        for instance in instances:
            if not instance.pk:
                instance.user = request.user
        instance.save()
        formset.save_m2m()

3 Answers 3

11

I have solved the first half of my question:

def save_formset(self, request, form, formset, change):
    if formset.model != InlineModel:
        return super(MainModelAdmin, self).save_formset(request, form, formset, change)
    instances = formset.save(commit=False)
    for instance in instances:
        if not instance.pk:
            instance.user = request.user
        instance.save()
    formset.save_m2m()

Now i'm interested in the bonus behavior:

  1. I'm required to select a user when adding a new inline due to validation rules. My best guess is to not include the 'user' field in my InlineModelInline.fields tuple, but then this won't show the author for existing InlineModel instances. (Edit: adding 'user' to readonly_fields works here)

  2. (Edit) How can I make the existing inlines render 'data' as readonly, but still be able to edit it when adding a new inline?

4
  • 2
    A note to myself, this save_formset is a method of the admin.ModelAdmin, and it will process all child Inlines assigned in the ModelAdmin.
    – monkut
    Aug 31, 2011 at 11:50
  • Thanks for your question/answers here. As far as the 'bonus' functionality -- after marking the user foreignKey in the django model with editable=False, the inline formset passed validation, the user field was still set and all was kosher. user = models.ForeignKey(User, editable=False)
    – sdailey
    Oct 21, 2013 at 3:38
  • Hm, totally forgot about editable=False in the model. Nice catch, thanks.
    – EB.
    Oct 27, 2013 at 17:29
  • This breaks deleting inline objects. Feb 27, 2017 at 20:58
1

It worked for me. This approach won't allow me to delete Inline items.

def save_formset(self, request, form, formset, change):
    for form in formset.forms:
        form.instance.user = request.user
    formset.save()
0

To answer the Bonus Question: "How can I make the existing inlines render 'data' as readonly, but still be able to edit it when adding a new inline?":

I use two inlines for the same model:

#admin.py
class InlineModelInline(admin.TabularInline):
    model = InlineModel
    extra = 1
    max_num = 1

#admin.py
class InlineModelExistingInline(admin.TabularInline):
    model = InlineModel
    readonly_fields = ('data', 'user') #All Fields here except pk
    can_delete = False
    extra = 0
    max_num = 0

class MainModelAdmin(admin.ModelAdmin):
    ...
    inlines = [InlineModelInline, InlineModelExistingInline]
    ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.