2👍
✅
I think you should refactor your code. As per your implementation, I believe name
field for `DirectReferral’ will be unique.
ref_user = User.objects.get(
username=form.cleaned_data['referrer']
)
user = User.objects.create_user(
username=form.cleaned_data['username']
)
# Count the referrer's direct referrals
ref_recruits = DirectReferral.objects.filter(referrer=ref_user).count()
# Get newly created direct referral
get_ref = DirectReferral.objects.get(name=user)
#Check if ref_recruits is not divisible by 2 or paired
if ref_recruits % 2 == 0:
get_ref.is_paired = True
DirectReferral.objects.filter(referrer=ref_user, is_paired=False).update(is_paired=True)
else:
get_ref.is_paired = False
get_ref.save()
0👍
You need to order your DirectReferral
objects somehow so they are returned in correct order, i.e. the last added one is the first one in a DirectReferral
objects list, then you can easily change the second object in a list, which is in your situation a previous one.
For example:
get_refs = DirectReferral.objects.filter(referrer=ref_user).ordery_by('-timestamp')
if ref_recruits % 2 != 0:
get_refs[0].is_paired = False
get_refs[0].save()
else:
get_refs[0].is_paired = True
get_refs[1].is_paired = True
get_refs[0].save()
get_refs[1].save()
- [Answered ]-Is there a way to have dictinary-like field in Django model?
- [Answered ]-Creating file from Django <InMemoryUploadedFile>
- [Answered ]-How to implement forget password functionality using Django and Python
- [Answered ]-Change Django Rest Framework serializers output structure?
Source:stackexchange.com