简体   繁体   English

当存在外键时,Django中的UserCreationForm具有额外的字段

[英]UserCreationForm in Django with extra fields when a foreign key exists

I am very new to Django and I am trying to create a registration form. 我是Django的新手,正在尝试创建注册表格。 This is the relevant part of my model : 这是我模型的相关部分:

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

    university = models.ForeignKey('University')
    course_title = models.ForeignKey('Course_Title')

    class Meta:
        db_table = 'user_profile'

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

post_save.connect(create_user_profile, sender=User)

This is the form I am using: 这是我正在使用的表格:

from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.contrib.auth.models import User
from shelf.models import University, Course_Title, UserProfile

class RegistrationForm(UserCreationForm):

error_messages = {
    'duplicate_email': "A user with that e-mail already exists.",
    'email_mismatch': "The two e-mail fields didn't match.",
}

# An e-mail is being used instead of the usual Django username.
username = forms.EmailField(label="E-mail")
username2 = forms.EmailField(label="Confirm E-mail")
university = forms.ModelChoiceField(queryset=University.objects.all(), empty_label="Please choose a university")
course_title = forms.ModelChoiceField(queryset=Course_Title.objects.all(), empty_label="Please choose a course title")

class Meta:
    model = User
    fields = ("first_name", "last_name", "university", "course_title", "username", "username2")

    .
    .
    .

def save(self, commit=True):
    user = super(RegistrationForm, self).save(commit=False)
    user.save()
    user_profile = UserProfile(user=user, university=self.cleaned_data["university"], course_title=self.cleaned_data["course_title"])
    if commit:
        user_profile.save()
    return user_profile

And, this is my view : 而且,这是我的观点

def index(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            new_user = form.save()
            return HttpResponseRedirect("/")
    else:
        form = RegistrationForm() 

    return render(request, "shelf/register.html",  {'form': form,  })

I get the error user_profile.university_id may not be NULL. 我收到错误user_profile.university_id可能不为NULL。 I have looked a lot for why this happens and I understand why it happens. 我已经为发生这种情况的原因进行了很多调查,并且了解发生这种情况的原因。 But I can't seem to fix it. 但是我似乎无法修复它。

Thanks in advance. 提前致谢。

It looks like the problem is caused by your signal handler: 看来问题是由您的信号处理程序引起的:

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

post_save.connect(create_user_profile, sender=User)

Please try disable this handler and test again. 请尝试禁用此处理程序,然后再次测试。

If you want the signal handler, please try changing the form.save method: 如果需要信号处理程序,请尝试更改form.save方法:

def save(self, commit=True):
    user = super(RegistrationForm, self).save(commit=False) #this should trigger the signal handler to create a UserProfile instance, but not commit
    user_profile = user.get_profile()
    user_profile.university=self.cleaned_data["university"]
    user_profile.course_title=self.cleaned_data["course_title"]
    if commit:
        user.save()
        user_profile.save()

Be careful, if you create a user in the shell, you still get the same error, I think maybe you could add default value for university and course_title field in UserProfile model: 请注意,如果在外壳中创建用户,仍然会遇到相同的错误,我认为您可以为UserProfile模型中的university和course_title字段添加默认值:

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

    university = models.ForeignKey('University', default=1)
    course_title = models.ForeignKey('Course_Title', default=1)

I've the same issue but couldn't solve it though I followed the steps 我有同样的问题,但是我按照以下步骤解决了

Model 模型

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save

class UserProfile(models.Model):
    url = models.URLField()
    home_address = models.TextField()
    user = models.ForeignKey(User, unique=True)


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

post_save.connect(create_user_profile, sender=User)

View 视图

from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User, Group
from django import forms
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.template.context import RequestContext
from django.views.decorators.csrf import csrf_protect
class RegisterForm(UserCreationForm):
    permission_choices = (
            ('1', 'test1'),
            ('2', 'test2'),
            )
    email      = forms.EmailField(required=False)
    first_name = forms.CharField(required=False)
    last_name  = forms.CharField(required=False)
    url        = forms.URLField()
    permission = forms.ChoiceField(choices=permission_choices)

    class Meta(UserCreationForm.Meta):
        model = User
        fields = ('first_name', 'last_name', 'url', 'username', 'email', 'password1', 'password2', 'permission')

    def save(self, commit=True):
        user = super(RegisterForm, self).save(commit=False)
        user_profile    = user.get_profile()
        user.email      = self.cleaned_data['email']
        user.first_name = self.cleaned_data['first_name']
        user.last_name  = self.cleaned_data['last_name']
        user.permission = self.cleaned_data['permission']
        user_profile.url = self.cleaned_data['url']
        if commit:
            user.save()
            user_profile.save()
        return user

@csrf_protect
def register(request):
    if request.method == 'POST':
        form = RegisterForm(request.POST)
        if form.is_valid():
            new_user = form.save()
            if new_user.permission == 'q':
                try:
                    new_user.groups.add(Group.objects.get(name='test1'))
                except Group.DoesNotExist:
                    u = User.objects.get(pk=new_user.pk)
                    u.delete()
                    return HttpResponse('The Organisation you\'ve just choosed doesn\' exist')
            return HttpResponse('OK')
    else:
        form = RegisterForm()

    return render_to_response('registration/user.html', {'form': form}, context_instance=RequestContext(request))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM