B
B
Boldy2015-01-07 13:53:06
Django
Boldy, 2015-01-07 13:53:06

How to get sum of all inline fields in django?

There are two classes:

class Foo(models.Model):
  ...
  my_max_total = models.IntegerField()

class Bar(models.Model):
  total = models.IntegerField()
  my_foo = models.ForeignKey(Foo)

class BarInline(admin.TabularInline):
    model = Bar

class FooAdmin(admin.ModelAdmin):
    inlines = (BarInline, )

How to check when editing a Bar, including inlines, that the total sum of totals of all Bars of one Foo does not exceed my_max_total specified in it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri Shikanov, 2015-01-07
@Boldy

Set a custom admin form and override its clean method. In this method, check everything you need and in case of an error, throw the forms.ValidationError exception.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question