0

I have a model with several fields. And I wanted to add another field in django-admin (list_display), where it is the sum of all fields.

Model's

class Model1(models.Model):
    field1 = models.DecimalField(blank=True,null=True,decimal_places=2,max_digits=8)
    field2 = models.DecimalField(blank=True,null=True,decimal_places=2,max_digits=8)
    field3 = models.DecimalField(blank=True,null=True,decimal_places=2,max_digits=8)
    field3 = models.DecimalField(blank=True,null=True,decimal_places=2,max_digits=8)
    field4 = models.DecimalField(blank=True,null=True,decimal_places=2,max_digits=8)


    def __str__(self):
        return str(self.id)

Model's Admin

class Model1Admin(admin.ModelAdmin):
    model = Model1

    list_display = ('field1','field2',<!--Sum of (field1,field2,field3,field4,field5-->)

admin.site.register(Modell,Model1Admin)

i am using the last version of django.

2
  • what did you try until now Commented Jun 12, 2020 at 12:43
  • i am trying to do a def on admin file using a queryset to return the value i want , but until now , nothing Commented Jun 12, 2020 at 12:46

2 Answers 2

1

if you want you can do something like that:

1- Create a property in your model:

class Model1(models.Model):
    field1 = models.DecimalField(blank=True,null=True,decimal_places=2,max_digits=8)
    field2 = models.DecimalField(blank=True,null=True,decimal_places=2,max_digits=8)
    field3 = models.DecimalField(blank=True,null=True,decimal_places=2,max_digits=8)
    field3 = models.DecimalField(blank=True,null=True,decimal_places=2,max_digits=8)
    field4 = models.DecimalField(blank=True,null=True,decimal_places=2,max_digits=8)


    def __str__(self):
        return str(self.id)

    @property
    def final_sum(self):
        return self.field1 + self.field2 + self.field3 + self.field4 + self.field5

2- Then use it in your admin.py

class Model1Admin(admin.ModelAdmin):
    model = Model1

    list_display = ('final_sum', 'field1','field2', )

admin.site.register(Modell,Model1Admin)
Sign up to request clarification or add additional context in comments.

Comments

1

From Django admin docs regarding list_display

There are four types of values that can be used in list_display:

....

A callable that accepts one argument, the model instance. For example:

def upper_case_name(obj):
    return ("%s %s" % (obj.first_name, obj.last_name)).upper()
upper_case_name.short_description = 'Name'

class PersonAdmin(admin.ModelAdmin):
    list_display = (upper_case_name,)

so in your case you can create method something like following

def field_sum(obj):
    attribute_list = [ 'field1','field2',...]
    return sum(getattr(obj, attribute) or 0 for attribute in attribute_list)

field_sum.short_description = 'Field sum'

3 Comments

that give a error "field_sum() takes 1 positional argument but 2 were given" , but i add 'self' on the def , and it is already working. thank you
the value that returns to me in my case is in thousands of euros, what will be the best strategy to present the result like xxx xxx,xx?
this is follow up question, do your research if you don't find answer post new question -> meta.stackoverflow.com/questions/266767/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.