my user_id profile database is null, how i insert the value in user_id?

89 views Asked by At

enter image description here

Why does my data look like this?

can i make the value in user_id? if i delete the def create_user_profile it will create nama, nik, email, nomor_hp. but user_id is 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

this is my 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?

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,
                '[email protected]',
                [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
            })
1

There are 1 answers

1
willeM_ Van Onsem On BEST ANSWER

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.

I advise to drop the signal that creates the 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. 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()
            # …
    # …