gpt4 book ai didi

Django:检查实例是否存在于用户数据库中并将其保存到应用程序模型数据库

转载 作者:行者123 更新时间:2023-12-02 07:07:46 24 4
gpt4 key购买 nike

我在我的 forms.py 中使用了这个

from django.contrib.auth.models import User
from .models import DirectReferral, IndirectReferral

class RegistrationForm(forms.Form):
username = forms.RegexField()
referrer = forms.CharField()

def clean_username(self):
try:
user = User.objects.get(username__iexact=self.cleaned_data['username'])
except User.DoesNotExist:
return self.cleaned_data['username']
raise forms.ValidationError(_("The username already exists."))

如何检查用户数据库中是否存在引用实例?

我执行了下面的代码,但在/register/处出现 TypeError 错误

    def clean_referrer(self):
try:
user = User.objects.get(username__iexact=self.cleaned_data['referrer'])
except User.DoesNotExist:
raise forms.ValidationError(_("User does not exist."))
return self.cleaned_data['referrer']

我也尝试这样做,但出现字符串索引必须是整数的错误。

    def clean(self):
if 'referrer' in self.cleaned_data:
if User.objects.filter(username=self.cleaned_data['referrer']).exists():
return self.cleaned_data['referrer']
else:
raise forms.ValidationError(_("Referrer doesn't exist!"))

这是我的 models.py

from django.contrib.auth.models import User

class DirectReferral(models.Model):
name = models.OneToOneField(User, primary_key=True)
referrer = models.ForeignKey(User, related_name="direct_referrals")

def __str__(self):
return self.name.username


class IndirectReferral(models.Model):
name = models.OneToOneField(User, primary_key=True)
referrer = models.ForeignKey(User, related_name="indirect_referrals")

def __str__(self):
return self.name.username

如何获取引荐来源网址实例数据?
这是我的views.py(已更新)

def register(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
user = User.objects.create_user(
username=form.cleaned_data['username']
)

ref_user = User.objects.get(
username=form.cleaned_data['referrer'])

direct = DirectReferral.objects.create(
name = user,
referrer = ref_user
)

if DirectReferral.objects.filter(referrer=user).exists():
indirect = IndirectReferral.objects.create(
name = user,
referrer = ref_user
)
return HttpResponseRedirect('/register/success/')

最佳答案

这就是您所需要的:

def register(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
user = User.objects.create_user(
username=form.cleaned_data['username'],
)

# Assuming form.cleaned_data['referrer'] has usernames.
# If it has pks then just use pk=form.cleaned_data['referrer']
# You should wrap this in a try-except
dr_user = User.objects.get(username=form.cleaned_data['referrer'])
direct_referral = DirectReferral.objects.create(
user=user,
referrer=dr_user,
)

# Wrap this in try-except as well.
ir_user = dr_user.direct_referral.referrer
indirect_referral = IndirectReferral.objects.create(
user=user,
referrer=ir_user,
)
return HttpResponseRedirect('/register/success/')

此外,这看起来像是 post_save 信号的工作。在这里查看:https://docs.djangoproject.com/es/1.9/ref/signals/#post-save

关于Django:检查实例是否存在于用户数据库中并将其保存到应用程序模型数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36670434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com