1

Django noob here - I was recently pointed to django-all-auth for registering/handling users, which I'm finding awesome in its near instant setup.

However I'm stumbling at trying to implement multiple user profile models. In reading other answers I've found this to be the closest answer thus far, but not really what I need.

When coding my own basic registration the user would select an account type (basic, pro, elite for example - each being their own profile model). Depending on the link selected the signup form would display both the generic User registration form as well as the profile form of the type chosen by the user.

I know I can go so far as to completely customize all-auth and make something like this work, but I'm hoping to be pointed in a direction that involves less wrecking of the original app. I've thought about having user redirected after signup to choose a profile type, but that seems to be a lot of extra steps.

Thanks for the help!

Community
  • 1
  • 1
Adam Croft
  • 25
  • 4

1 Answers1

0

To extend the basic user class, just subclass AbstractUser. You can find that in the docs. With this you can add the fields your basic user is missing.

Now, you want several types of profiles with different fields, or perhaps the same fields but adding new fields every time.

You can create something like:

class ProfileBase(models.Model):
    user=models.OneToOneField(User)

class ProfilePro(ProfileBase):
    pro_field=models.SomeField(Foo)

#You can extend ProfilePro here if you want it to include the pro_field
class ProfileElite(ProfileBase):
    elite_field=models.someField(Bar)

Once you have these models creating the forms should be easy. Be aware, when you subclass this way django creates 1 table per model, including on the subclass table only the new fields. This makes necessary a join per level of inheritance so try not to abuse that.

There is a second way to use inheritance:

class ProfileBase(models.Model):
    user=models.OneToOneField(User)

    class Meta:
        abstract=True

If you define the base class as abstract you won't have a table for it, so you can't create objects for that model, but each of your subclasses will be on it's own table. If you do it this way you may need extra logic to handle cases when user changes of type of profile (i.e. went from pro to elite).

To keep this separated from django-allauth, just finish the registration (create a form and in your settings define ACCOUNT_SIGNUP_FORM_CLASS to override all-auth default with your basic info + pick a profile type form) and once your user is logged in, redirect them to their profile to finish the process.

cdvv7788
  • 2,021
  • 1
  • 18
  • 26
  • Right, I understand inheritance and how I can subclass off a base class... I guess what I'm trying to wrap my head around more is the flow of the forms. I'm unsure as to how to implement the class choice within the form. Originally I had a static page with links where the URLconf used regex to pull the correct form based off of a variable within the link. But with allauth there doesn't seem to be an elegant way to pull that sort of thing off without really customizing the original views and templates. That's what I'm not sure on. Thanks for the help! – Adam Croft Feb 10 '15 at 16:04
  • You are trying to have several signup forms instead of a single signup and then an extra profile settings. If you need a different profile for different type of accounts, you can do that after signup (assigning that on signals or when user visits it's profile). If you require the info, add a mixin to redirect to their profile until the information has been updated. – cdvv7788 Feb 10 '15 at 23:13