0
votes

I got a model containing two counters :

    class MyObject(models.Model):
        ...
        student_c = models.PositiveIntegerField(default=0, blank=True, null=True)
        company_c = models.PositiveIntegerField(default=0, blank=True, null=True)

    def total_followers(self):
        return self.student_c + self.company_c

one is working perfectly and returns an integer value but the other one returns :

django.db.models.expressions.ExpressionNode object at 0x7ff4d8320a10

I tried to change PositiveIntegerField to IntegerField and to rename the field with another name but I still get the same result ? I destroyed the database and rebuilt it many times

In my template I can see this : (+: (DEFAULT: ), 1)

In fact I'm trying to do an atomic increment like this:

project = get_object_or_404(Project, pk=kwargs['pk'])
project.student_c = F('student_c') + 1
project.save(update_fields=['student_c']) or even just project.save()

This is where the problem is coming from. I looked there and I found the same code : Atomic increment of a counter in django

What am I doing wrong ?

Any help would be very appreciated.

Thanks

2
What is the code that returns an ExpressionNode?jproffitt
I put a debugg trace in my "total_followers" method and when I call "self.company_c" I got an integer but "self.student_c" returns an ExpressionNode object (I checked my sql schema both fields are identical)samidarko

2 Answers

0
votes

I don't know if this is similar to your problem, but maybe can help you

http://www.voidspace.org.uk/python/weblog/arch_d7_2011_04_30.shtml

Maybe you need to call .save before call total_followers in your view...

For example:

import model.MyObject

myObj = MyObject.objects.create(....)
myObj.save()

myObj.total_followers()
0
votes

I'm not sure you're using the F() object correctly. Have you tried just doing

Projects.objects.filter(pk=kwargs['pk']).update(student_c=F('student_c') + 1)

That would replace those three lines. Also you could try this for the second line:

project.student_c = project.student_c + 1