1

I have a custom UserProfile:

class UserProfile(models.Model):
    BUYER_USER = 1
    CLIENT_USER = 2
    ARTIST_USER = 3
    ADMIN_USER = 3
    USER_CHOICES = (
        (BUYER_USER, 'Buyer'),
        (CLIENT_USER, 'Client'),
        (ARTIST_USER, 'Artist'),
        (ADMIN_USER, 'Admin'),
    )

    # This field is required.
    user = models.OneToOneField(User)

    usertype = models.IntegerField(choices=USER_CHOICES, default=BUYER_USER)
    dob = models.DateField(blank=True, null=True)
    address = models.TextField(max_length=8, blank=True, null=True)

I am using django-registration-redux to manage accounts. The built-in registration view has following layout:

enter image description here

But I would like to move to the following layout:

enter image description here

I know how to change the template and adapt the layout for the fields related to the User model, but I do not know how to tell django-registration-redux to populate the UserProfile based on the data entered in the RegistrationView form.

Is this at all possible? How?

blueFast
  • 41,341
  • 63
  • 198
  • 344

1 Answers1

1

What I think could be a possible answer is to create a form for the UserProfile, add it to the redux registration view. Locate the view function and see where they are checking for the form is being submitted with is_valid(), and add your Userform to the if block, to be check if it's valid too. To be able to populate UserProfile with the redux registration form you can do the following

user_form.save(commit=false)
user_form.user = registration_form.user
user_form.save()

In that way the UserProfile user field is bein populated with the user field from the registration model.

Apologies if this is completely wrong, as I am not fimiliar with registration-redux package, but this is how I usually do it.

Here is a github gist on how to do it with class based views: https://gist.github.com/michelts/1029336

In this case you need to edit registration-redux registration/views.py https://github.com/macropin/django-registration/blob/master/registration/views.py#L68-L119

qasimalbaqali
  • 2,079
  • 24
  • 51
  • Thanks! I have to wrap my head around this: I have seen that the `form_valid` and `register` methods accept just one form. How could I work with two forms here? – blueFast Dec 07 '15 at 10:10
  • @gonvaled Turns out CBV's can't handle two forms at once, but you can handle a two forms on separate validation http://stackoverflow.com/a/15499249/4708186 If you need two forms at once you'll need to use a Function View, but in this case I don't think you need that. Just do as the codeblock that I wrote when a user is registering and then maybe redirect them to a Userprofile form page to fill out the rest? Just an alternative suggestion. Or even you can us FV to validate both the registration-redux form and your userprofile form! – qasimalbaqali Dec 07 '15 at 14:05
  • Still thinking about this: actually, I do not need two forms, but one single form which will be split into two models. – blueFast Dec 07 '15 at 17:27
  • @gonvaled I think I found what you're looking for http://stackoverflow.com/questions/2601487/django-registration-django-profile-using-your-own-custom-form/3298481#3298481 He's using a User model and a UserProfile model in one form. I guess what you're trying to do too. – qasimalbaqali Dec 07 '15 at 17:31