Django:更新或更改先前保存的模型数据。

时间:2021-08-16 19:23:39

I have this code in my forms.py

我的表单中有这个代码

    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:

        # Newly created direct referral is_paired is False
        get_ref.is_paired = False
        get_ref.save()

    else:

        # Newly created direct referral is_paired is True
        get_ref.is_paired = True
        get_ref.save()

        # But I also want to update the previous is_paired to True 
        # of the same referrer

How do I update the previous is_paired to True of the same referrer?

如何将先前的is_pair更新为同一引用者的True ?

Check image below to have a better understanding of what I mean. Django:更新或更改先前保存的模型数据。 I hope you understand.

查看下面的图片以更好地理解我的意思。我希望你理解。

models.py(requested)

models.py(请求)

class DirectReferral(models.Model):
    name = models.OneToOneField(settings.AUTH_USER_MODEL, primary_key=True)
    referrer = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="direct_referrals")
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    is_paired = models.NullBooleanField(null=False)

2 个解决方案

#1


2  

I think you should refactor your code. As per your implementation, I believe name field for `DirectReferral' will be unique.

我认为你应该重构你的代码。根据您的实现,我相信“DirectReferral”的名称字段将是惟一的。

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()

#2


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.

您需要顺序排列您的DirectReferral对象,以便它们以正确的顺序返回,即最后一个添加的对象是DirectReferral对象列表中的第一个对象,然后您可以轻松地更改列表中的第二个对象,这在您的情况中是前一个。

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()

#1


2  

I think you should refactor your code. As per your implementation, I believe name field for `DirectReferral' will be unique.

我认为你应该重构你的代码。根据您的实现,我相信“DirectReferral”的名称字段将是惟一的。

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()

#2


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.

您需要顺序排列您的DirectReferral对象,以便它们以正确的顺序返回,即最后一个添加的对象是DirectReferral对象列表中的第一个对象,然后您可以轻松地更改列表中的第二个对象,这在您的情况中是前一个。

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()