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.