I'm a bit stuck on a problem. I googled a lot, but didn't find an answer.
I have a "Loan" model and a "Payment" model. In the admin, I display the payments for each loan in inlines. I want payements that are more than 3 days old to be displayed as readonly, as they are considered to be archived.
I don't need it to be secure (I'm doing checks in the model save/delete methods anyway), but I need to have them display with the readonly html attribute so that they aren't too easily modified.
Here's what I've got so far :
class PaymentFormSet(BaseInlineFormSet):
@cached_property
def forms(self):
forms = super().forms
for form in forms:
if form.instance.is_archived:
form.base_fields['amount'].widget.attrs['readonly'] = True
form.base_fields['date'].widget.attrs['readonly'] = True
return forms
class PaymentInline(admin.TabularInline):
model = Payment
formset = PaymentFormSet
...
This raises no exception (which means the widget.attrs dictionarry is correctly referenced), but the attribute doesn't display. The form is exactly the same than without...
I tried some to replace the widget too :
...
form.base_fields['amount'].widget = TextInput(attrs={'readonly':True})
...
and the field :
...
form.base_fields['amount'] = django.forms.CharField(widget=TextInput(attrs={'readonly':True}))
...
But all those don't produce any effect.
I considered other options (InlineModelAdmin's readonly_fields...) but had even more problems (as then the data isn't posted, I was getting validation errors), and I think it's not the right way to go.
After that, I'd love to be able to hide the "delete" button too...
Thank you very much in advance !!
Olivier