Tuesday, October 21, 2014

how to save in inline form total sum

Hi, i need sum inlines field but i no have idea

example for more clean, i have 2 models

in models.py i have:

class ModelA(models.Model):
    field1 = charfield(...)
    field2 = integeterfield()
    .....

CHOICE_TYPE = ((1,"option1"),(2,"option2"),(3,"option3"))

class ModelB(models.Model):
    choice = models.Integerfield(choices='CHOICE_TYPE')
    men = models.IntegerField()
    women = models.IntegerField()

    total_men = integerField(editable=False)
    total_women = integerField(editable=False)

    fk_model_a = fk(ModelA)

    def save(self, *args, **kwargs): #this is for sum all 3 data put in admin inlines
        self.total_men += self.men
        self.total_women += self.women
        super(ModelB, self).save(*args, **kwargs)

in admin.py :

class ModelBInline(admin.TabularInline):
    model = ModelB
    extra = 3
    max_num = 3

class ModelAAdmin(admin.ModelAdmin)
    inlines = [ModelBInline]


admin.site.register(ModelA, ModelAAdmin)

but this override save not working, this error when save datas
unsupported operand type(s) for +=: 'NoneType' and 'int'
and the line error is this:   self.total_men += self.men


any idea for this working.

Cheers

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAM-7rO0u%3Dw6hyYjriE%3D5KbBH-k_LJAY0MEAiTxh0jwAx0Td8CA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment