0

Trying to connect Django form and extended user model, but struggling to get the form to save. Basically, I'm able to authenticate user, but not save information to the CustomUser model or "profile" model for the user.

I feel like I fundamentally don't understand how to extend the user model, so if you have some advice to that affect that would be very helpful.

#views.py
def registration(request):
    if request.method == "POST":
        form = CustomUserCreationForm(request.POST)
        if form.is_valid():
            custom = form.save(commit=False)
            user = request.user 
            custom = user.profile()
            custom.key = "".join(random.choice(string.digits) for i in range(20))
            custom.save()
            username = form.cleaned_data['username']
            password = form.cleaned_data['password2']
            user = authenticate(request, username=username, password=password)
            messages.success(request, f'Your account has been created! Please proceed to agreements and payment.')
            return redirect('other_page')
    else:
        form = CustomUserCreationForm(request.POST)
    return render(request, 'registration.html', {'form': form})

Here is my models.py

class CustomUser(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    key = models.CharField(max_length=25, null=True, blank=True)
    first_name = models.CharField(max_length=25)
    last_name = models.CharField(max_length=25)
    username = models.CharField(max_length=25)
    password1 = models.CharField(max_length=25)
    password2 = models.CharField(max_length=25)

    @receiver(post_save, sender=User)
    def create_custom_user(sender, omstamce, created, **kwargs):
        if created:
            CustomUser.objects.create(user=instance)

    @receiver(post_save, sender=User)
    def save_user_profile(sender, instance, **kwargs):
        if instance.CustomUser.save()

    def __str__(self):   
        return f'{self.first_name} ({self.last_name})'

In settings.py I added "AUTH_PROFILE_MODULE = 'app.CustomUser'". However, I'm getting this error:

AttributeError: 'User' object has no attribute 'profile'

1 Answers1

0

AUTH_PROFILE_MODULE is deprecated since 1.7. It and the get_profile() method on the User model are removed. You should use AUTH_USER_MODEL

The detailed explanation can be found here https://docs.djangoproject.com/en/2.2/topics/auth/customizing/

Btw, if you just starting a new project it's highly encouraged to create custom user model with AbstractUser

If you want different behavior for some users, then I'd say groups and permissions are nice choices for you https://docs.djangoproject.com/en/2.2/topics/auth/default/#topic-authorization

Artem Kolontay
  • 840
  • 1
  • 10
  • 16
  • I tried to do this, but ran into a whole host of problems, because the majority of my users will not have a profile, but registered users will. I want to use the Django base user model for the majority and for a small proportion of users - this custom model. –  Aug 11 '19 at 13:52
  • Well, you can't have multiple models for auth, only one. Could you please provide a more detailed use case of what you want? – Artem Kolontay Aug 11 '19 at 14:12
  • Sure, so I have users who do not register, who can look at not blocked content, and users that register, who can look at blocked content. I need a user model flexible enough to handle both users. When I try to build a custom model, it either forces all users into the base model or custom model. Hence, for this use case I need to extend the base model. –  Aug 11 '19 at 14:23
  • this was my old custom user -mode - tried the suggestion about AbstractBaseUser, but it did not work:https://stackoverflow.com/questions/57446355/custom-user-model-error-attributeerror-customuser-object-has-no-attribute-i –  Aug 11 '19 at 14:24
  • I updated my answer. Looks like you actually need permissions and not different user models. – Artem Kolontay Aug 11 '19 at 15:38
  • I think the problem is with changing to custom user midproject:https://ruddra.com/posts/django-custom-user-migration-mid-phase-project/ –  Aug 11 '19 at 18:25