为什么我的数据看起来像这样?
我可以在 user_id 中设置值吗?如果我删除 def create_user_profile,它将创建 nama、nik、email、nomor_hp。但 user_id 为空
在图片中,我在没有 def create_user_profile 的情况下创建了 id 1 和 2,当我使用了用户 def create_user_profile 时创建了 id 3,4,5,6
这是我的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)
这是我创建用户的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
})