I have an Inline Model in Django model admin , and I need to create a condition before saving the items, here is the code am using :
class PRCItemInline(admin.TabularInline):
def get_form(self, request, obj=None, **kwargs):
form = super(PRCItemInline, self).get_form(request, obj, **kwargs)
form.base_fields['product'].widget.attrs['style'] = 'width: 50px;'
return form
ordering = ['id']
model = PRCItem
extra = 1
autocomplete_fields = [
'product',
'supplier',
]
fields = (
'product', # 1
'quantity', # 2
'unitary_value_reais_updated', # 4
'issuing_status',
'approval_status',
'receiving_status',
)
readonly_fields = ['issuing_status',
'approval_status',
'receiving_status',
]
def save_formset(self, request, form, formset, change):
obj = form.instance
if obj.purchase_request.is_analizer:
return HttpResponse("You can't change this")
else:
obj.save()
As you see, I used the save_formset
method to be able to reach the fields of the model, and then filter based on it. but it just saves the items no matter the If statement I added.