i want to interate over queryset and if there is a new user added, i want to add some points for a specific user. To be more clear with what i am building: => I am writing a referral system logic in django. The referral system works fine now, but what i want to implement is this. When i refer a user which are “user 2” and “user 3”, it is stored that i have refered two users, now i have also implemented a way to know if “user 2” or “user 3” which are refered by me: have gotten a new user refered by them (“user 2” or “user 3”).
Now what i want to achieve is this, when there is a new user from “user 2” or " user 3 " i want to add a some points to the “user 1” which refered the two user (“user 2” and “user 3”) that referred the new users.
I am thinking of using a forloop to iterate over the
second_level_recommended, then add the point to the profile but it doesn’t seem to work or maybe the code that i wrote was wrong. This is the code that i wrote for the foorloop below
for i in second_level_recommended: profile = Profile.objects.filter(recommended_by=request.user) profile.indirect_sign_up = profile.indirect_sign_up =+ 200
This is the code to check for the new users that “user 2” and “user 3” have reffered belonging to “user 1”
my_recomended = Profile.objects.filter(recommended_by=request.user).values_list('user__id', flat=True) second_level_recommended=Profile.objects.filter(recommended_by__in=my_recomended)
Then there is a forloop in the templates to display the
I have also tried using django
F() expressions but it does seem to work too (Code Below)
from django.db.models import F my_recomended = Profile.objects.filter(recommended_by=request.user).values_list('user__id', flat=True) second_level_recommended=Profile.objects.filter(recommended_by__in=my_recomended) second_level_recommended.update(indirect_sign_up=F('indirect_sign_up') + 200)
NOTE: There is a
Profile model which have these fields in it
class Profile(models.Model): user = models.OneToOneField(User, ...) ... indirect_sign_up = models.IntegerField(default=0) code = models.CharField(max_lenght=100, ...) recommended_by = models.ForeignKey(User, ...)
What could be the best way to achive??