Answer the question
In order to leave comments, you need to log in
How to display readonly field values in ChangeForm in django?
There is a payout model. You need to prohibit changing, but be able to view all its fields in CashPaymentChangeForm. How to do it?
If it is poorly formulated - after the model is created - you cannot change it, but in ChangeForm you can see all its fields in readonly.
The model itself:
class ClientToCompanyPayment(models.Model):
total = models.PositiveIntegerField(verbose_name='Сумма платежа', blank=False, null=False)
date = models.DateField(verbose_name='Дата платежа', blank=False)
client = models.ForeignKey(Client, verbose_name='Клиент')
payment_id = models.CharField(verbose_name='ID платежа', max_length=40, blank=False,
null=False, unique=True)
def __str__(self): # __unicode__ on Python 2
return self.payment_id
class Meta:
abstract = True
class CashPayment(ClientToCompanyPayment):
class Meta:
db_table = 'cash_payments'
class CashPaymentCreationForm(forms.ModelForm):
class Meta:
model = CashPayment
fields = ('client', ) # wat?
def save(self, commit=True):
payment = super(CashPaymentCreationForm, self).save(commit=False)
if commit:
payment.save()
return payment
class CashPaymentChangeForm(forms.ModelForm):
class Meta:
model = CashPayment
fields = ()
def save(self, commit=True):
payment = super(CashPaymentChangeForm, self).save(commit=False)
if commit:
payment.save()
return payment
class CashPaymentAdmin(admin.ModelAdmin):
form = CashPaymentChangeForm
add_form = CashPaymentCreationForm
admin.site.register(CashPayment, CashPaymentAdmin)
Answer the question
In order to leave comments, you need to log in
Don't touch the form. You can override the get_readonly_fields method in the admin class. It looks like it might look like this:
class CashPaymentAdmin(admin.ModelAdmin):
# ...
def get_readonly_fields(self, request, obj=None):
if obj:
return ['total', 'date', 'client', 'payment_id']
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question