Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit field from ForeignKey in Admin

class BoxItem(models.Model):
    quantity = models.IntegerField()

class Box(models.Model):
    name = models.CharField(max_lenght=150)
    item = models.ForeignKey(BoxItem)

admin:

admin.site.register(BoxItem)
admin.site.register(Box)

How can I add possibility to edit BoxItem.quantity value in Box single object admin page?

like image 297
user3494808 Avatar asked Apr 20 '26 17:04

user3494808


1 Answers

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.)

like image 169
frnhr Avatar answered Apr 22 '26 06:04

frnhr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!