[英]my user_id profile database is null, how i insert the value in user_id?
Why does my data look like this?为什么我的数据看起来像这样?
can i make the value in user_id?我可以在 user_id 中设置值吗? if i delete the def create_user_profile it will create nama, nik, email, nomor_hp.如果我删除 def create_user_profile,它将创建 nama、nik、email、nomor_hp。 but user_id is null但 user_id 是 null
in the picture, id 1 and 2 i created without def create_user_profile and id 3,4,5,6 is created when i user def create_user_profile在图片中,我在没有 def create_user_profile 的情况下创建了 id 1 和 2,当我使用了用户 def create_user_profile 时创建了 id 3,4,5,6
this is my models.py这是我的models.py
class Profile(models.Model):
user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
nama = models.CharField(max_length=30, blank=True)
nik = models.CharField(max_length=30, blank=True)
email = models.EmailField(max_length=75, blank=True)
nomor_hp = models.TextField(max_length=15, blank=True)
def __str__(self):
return self.user
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
try:
instance.profile.save()
except ObjectDoesNotExist:
Profile.objects.create(user=instance)
this is my views.py to create a user, did i made a mistake here?这是我创建用户的views.py,我在这里犯了错误吗?
def signup(request):
if request.method == 'POST':
user_form = UserCreationForm(request.POST)
profile_form = ProfileForm(request.POST)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save(commit=False)
profile = profile_form.save(commit=False)
user.is_active = False
user.save()
profile.save()
uidb64 = urlsafe_base64_encode(force_bytes(user.pk))
domain = get_current_site(request).domain
link=reverse('activate', kwargs={
'uidb64': uidb64, 'token': token_generator.make_token(user)})
activate_url = 'http://'+domain+link
email_body = 'Hallo '+user.username + 'Tolong gunakan link ini untuk verifikasi akun anda\n' +activate_url
email_subject = 'Aktivasi Akun Anda'
email = EmailMessage(
email_subject,
email_body,
'noreply@kantorkecamatanbintanutara.com',
[profile.email],
)
email.send(fail_silently=False)
return redirect('/pengurusan/signin')
else:
return render(request, 'pengurusan/register.html', {
'user_form': user_form,
'profile_form': profile_form
})
Both your signup
view and the signal makes a Profile
object, and to make matters even worse, in the latter you do not link to the user.您的signup
视图和信号都生成了Profile
object,更糟糕的是,在后者中,您没有链接到用户。
I advise to drop the signal that creates the Profile
.我建议放弃创建Profile
的信号。 It has not much use.它没有多大用处。 If you later edit the Profile
, you should simply safe it, furthermore you should ensure that when you create a User
object, you simply link it properly.如果您稍后编辑Profile
,您应该简单地保护它,此外,您应该确保在创建User
object 时,您只需正确链接它。 For example with:例如:
def signup(request):
if request.method == 'POST':
user_form = UserCreationForm(request.POST)
profile_form = ProfileForm(request.POST)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
profile_form.instance.user = user
profile = profile_form.save()
# …
# …
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.