[Django]-Edit field from ForeignKey in Admin

3👍

In admin.py:

class BoxAdminForm(forms.ModelForm):
    box_item_quantity = forms.IntegerField()

    def __init__(self, *args, **kwargs):
        """ populate box_item_quantity initial value """
        super(BoxAdminForm, self).__init__(*args, **kwargs)
        if self.instance:
            self.initial['box_item_quantity'] = self.instance.item.quantity

    class Meta:
        model = Box


class BoxAdmin(admin.ModelAdmin):
    form = BoxAdminForm

    def save_model(self, request, obj, form, change):
        super(BoxAdmin, self).save_model(request, obj, form, change)
        obj.item.quantity = form.cleaned_data.get('box_item_quantity')
        obj.item.save()


admin.site.register(Box, BoxAdmin)

This works because your BoxItem field is required. Otherwise, there might be some more logic needed to handle cases when Box has no BoxItem associated.


I must say, even though it’s not in the question:
This model schema looks very weird to me. One BoxItem instance can be associated with many Box instances. Changing the quantity in one BoxItem can affect other Box instances as well, because they can be associated with the same BoxItem.
(This has nothing to do with the admin, it’s true no matter the way that change to quantity field happens.)

👤frnhr

Leave a comment