how to save in inline form total sum

163 views
Skip to first unread message

carlos

unread,
Oct 21, 2014, 11:23:17 PM10/21/14
to django...@googlegroups.com
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

Collin Anderson

unread,
Oct 23, 2014, 8:15:53 AM10/23/14
to django...@googlegroups.com
Hello,

I'm not sure what you're trying to do, but to fix the None error set default=0 on the total columns.

Or do something like this:
self.total_men = (self.total_men or 0) + (self.men or 0)

Thanks,
Collin

carlos

unread,
Oct 24, 2014, 1:04:57 AM10/24/14
to django...@googlegroups.com, cmawe...@gmail.com
Hi +Collin the idea is sum all data for example de model is
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 the admin look liked this:

ModelB in inline
choice               men           women
option1               6                   4
option2               1                   3
option3               2                  1

i need save in other field total this sum for example men = 9  and women total is = 8

for this reason i create 2 field more 
 total_men = integerField(editable=False)
 total_women = integerField(editable=False)

and create the override save method
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)

but no working :(

any idea to save the sums of the inlines options

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...@googlegroups.com.
To post to this group, send email to django...@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/a9f82650-d1dd-4350-80e7-22ad491c6aa9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Collin Anderson

unread,
Oct 24, 2014, 8:34:22 AM10/24/14
to django...@googlegroups.com, cmawe...@gmail.com
Hello,
 
ModelB in inline
choice               men           women
option1               6                   4
option2               1                   3
option3               2                  1

i need save in other field total this sum for example men = 9  and women total is = 8

Right, it seems to me that the "total"  need to be on a separate model from the individual counts, maybe on ModelA?

Collin

carlos

unread,
Oct 24, 2014, 12:26:05 PM10/24/14
to django...@googlegroups.com
Ok, i try en modelA create the total, thank for idea

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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

carlos

unread,
Oct 28, 2014, 2:46:20 PM10/28/14
to django...@googlegroups.com
Hi, +Collin
i update my ModelA and now is this:

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

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

    def save(self, *args, **kwargs): #this is for sum all 3 data put in admin inlines
        self.total_men = sum(map(sum, ModelB.objects.filter(choice__in = [1,2,3], modela_id = self.id).values_list('men',)))
        self.total_total_women = sum(map(sum, ModelB.objects.filter(choice__in = [1,2,3], modela_id = self.id).values_list('women',)))
        
        super(ModelA, self).save(*args, **kwargs)

the only problem that is save correct total sum but a hit 2 o 3 time button save in the admin, not first time
why? i don't know :(

any idea

Cheers

Collin Anderson

unread,
Oct 29, 2014, 2:32:02 PM10/29/14
to django...@googlegroups.com
Hello,

the only problem that is save correct total sum but a hit 2 o 3 time button save in the admin, not first time
why? i don't know :(

It's probably because ModelA is saved before the individual ModelB's.

You could calculate this _after_ the ModelB inlines(?) have been saved using the save_related hook.

Collin

Reply all
Reply to author
Forward
0 new messages