两个模型的CreateView

时间:2016-02-08 00:24:40

标签: python django django-views

我正在尝试为两个模型创建注册和登录视图。

我扩展了我的用户模型,并且我想知道如何在CreateView中使用扩展以及基本用户模型。我的自定义扩展程序如下所示(在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)

我正在使用django内置的auth系统,但是我很难理解如何在一个视图中使用两个模型,然后将表单放在模板中。

我试过这个(在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']

但这给了我一个错误(见http://dpaste.com/0PRZX2R) 所以我很难知道如何做到这一点。英语不是我的母语,所以原谅可怜的解释。

1 个答案:

答案 0 :(得分:1)

使用2表格和前缀,     class PrimaryForm(ModelForm):         类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