B
B
Boldy2014-11-02 23:56:15
Django
Boldy, 2014-11-02 23:56:15

How to prevent changing existing objects in Inline's in django?

There is a "client" model for which the first payment is created during creation. How to make it so that in the form of changing the client it was impossible to edit, but it was possible to view the inline of those payments that were created?

class CashPaymentAdmin(admin.ModelAdmin):

    def get_readonly_fields(self, request, obj=None):
        if obj:
            return ['total', 'date', 'client', 'id']
        else:
            return []

class CashPayment(models.Model):

    total = models.PositiveIntegerField(verbose_name='Сумма платежа', blank=False, null=False)
    date = models.DateField(verbose_name='Дата платежа', blank=False)
    client = models.ForeignKey(TreeNode, verbose_name='Клиент')

class CashPaymentInline(admin.StackedInline):
    model = CashPayment
    extra = 1
    formset = RequiredInlineFormSet
    can_delete = False

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2014-11-03
@Boldy

If just for the admin panel and only you will have access, then

class CashPaymentAdmin(admin.ModelAdmin):
    readonly_fields = ('total',)

And if it is somewhere else, then override the save method of the model, supplement it with checks, change the admin templates, or better, write your own admin panel. The same safety.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question