Sebastian Olsen
Sebastian Olsen

Reputation: 10878

CreateView for two models

I'm trying to create a registration and login view for two models.

I have extended my user model, and I'd like to know how to use the extensions along with the base User model inside a CreateView. My custom extension looks like this (in models.py):

class UserProfile(models.Model):

....

user = models.OneToOneField(User)
display_name = models.CharField(max_length=50, default="null")
avatar = models.ImageField(upload_to=generate_user_folder_avatar,storage=OverwriteStorage(),validators=[is_square_png])

usertype_choices = [
    ('PR', 'Producer'),
    ('ME', 'Mastering Engineer'),
    ('CP', 'Composer'),
    ('SI', 'Singer'),
    ('AR', 'Artist'),
    ('OT', 'Other'),
]

usertype = models.CharField(max_length=2,
                             choices=usertype_choices,
                             default='PR')
daw_choices = [
    ('FL', 'FL Studio'),
    ('AB', 'Live'),
    ('BT', 'Bitwig Studio'),
    ('CS', 'SONAR X3'),
    ('CB', 'Cubase'),
    ('AP', 'Apple Logic'),
    ('RE', 'Reason'),
    ('SO', 'Sony ACID'),
    ('PR', 'Pro Tools'),
    ('ON', 'Studio One'),
    ('MT', 'Digital Performer'),
    ('SA', 'Samplitude'),
    ('MC', 'Mixcraft'),
    ('RP', 'Reaper'),
    ('AR', 'Ardour'),
    ('OT', 'Other'),
    ('NO', 'None'),
]

daw = models.CharField(max_length=2,choices=daw_choices,default='NO')
usergenre = models.CharField(max_length=20,blank=True)
birthday = models.DateField()

joined = models.TimeField(auto_now=True,auto_now_add=False)
followers = models.ManyToManyField(User, related_name="followers",blank=True)
status = models.TextField(max_length=300,blank=True)
pro = models.BooleanField(default=False)

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)

post_save.connect(create_user_profile, sender=User)

I'm using django's built in auth system, but I am stumped on how to use two models in one view, and then put the forms in the template.

I tried this (in views.py):

from .models import UserProfile
from django.contrib.auth.models import User

class RegisterView(CreateView):
model = ['User', 'UserProfile']
fields = ['username','password','display_name','avatar','usertype','birthday','daw','usergenre']

But that gave me an error (see http://dpaste.com/0PRZX2R) So I'm stumped on how to do this. English is not my mother tongue so excuse the poor explanation.

Upvotes: 1

Views: 654

Answers (1)

Denis
Denis

Reputation: 11

Use 2 Form and prefix, class PrimaryForm(ModelForm): class Meta: model = Primary

class BForm(ModelForm):
    class Meta:
        model = B
        exclude = ('primary',)

class CForm(ModelForm):
     class Meta:
        model = C
        exclude = ('primary',)

def generateView(request):
    if request.method == 'POST': # If the form has been submitted...
        primary_form = PrimaryForm(request.POST, prefix = "primary")
        b_form = BForm(request.POST, prefix = "b")
        c_form = CForm(request.POST, prefix = "c")
        if primary_form.is_valid() and b_form.is_valid() and c_form.is_valid(): # All validation rules pass
                print "all validation passed"
                primary = primary_form.save()
                b_form.cleaned_data["primary"] = primary
                b = b_form.save()
                c_form.cleaned_data["primary"] = primary
                c = c_form.save()
                return HttpResponseRedirect("/viewer/%s/" % (primary.name))
        else:
                print "failed"

    else:
        primary_form = PrimaryForm(prefix = "primary")
        b_form = BForm(prefix = "b")
        c_form = Form(prefix = "c")
 return render_to_response('multi_model.html', {
 'primary_form': primary_form,
 'b_form': b_form,
 'c_form': c_form,
  })

Click

Upvotes: 1

Related Questions