为选定的用户类型创建自定义配置文件

时间:2019-06-18 23:27:21

标签: django

1。我的项目中有多种类型的用户。我已经创建了一个注册页面。在注册页面中,我可以选择用户类型。根据选择,应创建一个相关的配置文件,并将用户类型登录到其特定的配置文件页面中。例如,如果选择的用户类型为学生,然后在注册后应创建一个student_profile,并应将学生登录。.类似专业和其他用户类型。我使用django auth用户模型创建了注册。 2.对于每种用户类型,我都有许多自定义配置文件表单,不知道如何将其集成..model如下: 什么

请不要向我建议此链接,因为我对每种用户类型都使用注册,但是我对每种用户类型都只使用一个注册。 https://simpleisbetterthancomplex.com/tutorial/2018/01/18/how-to-implement-multiple-user-types-with-django.html

forms.py中的用户注册:

   class UserRegistrationForm(forms.ModelForm):
    password = forms.CharField(label='Password',
                               widget=forms.PasswordInput)
    password2 = forms.CharField(label='Repeat password',
                                widget=forms.PasswordInput)
    class Meta:


    model = User
    fields = ('username', 'first_name', 'email')

def clean_password2(self):
    cd = self.cleaned_data
    if cd['password'] != cd['password2']:
        raise forms.ValidationError('Passwords don\'t match.')
    return cd['password2']

def clean_email(self):
    email = self.cleaned_data.get('email')
    username = self.cleaned_data.get('username')
    if email and User.objects.filter(email=email).exclude(username=username).exists():
        raise forms.ValidationError(u'Email addresses must be unique.')
    return email

views.py:

@login_required
def dashboard(request):
    # Display all actions by default
    actions = Action.objects.exclude(user=request.user)
    following_ids = request.user.following.values_list('id',
                                                       flat=True)
    if following_ids:
        # If user is following others, retrieve only their actions
        actions = actions.filter(user_id__in=following_ids)
    actions = actions.select_related('user', 'user__profile')\
                     .prefetch_related('target')[:10]

    return render(request,
                  'account/dashboard.html',
                  {'section': 'dashboard',
                   'actions': actions})


def register(request):
    if request.method == 'POST':
        user_form = UserRegistrationForm(request.POST)
        if user_form.is_valid():
            # Create a new user object but avoid saving it yet
            new_user = user_form.save(commit=False)
            # Set the chosen password
            new_user.set_password(
                user_form.cleaned_data['password'])
            # Save the User object
            new_user.save()
            # Create the user profile
            Profile.objects.create(user=new_user)
            create_action(new_user, 'has created an account')
            return render(request,
                          'account/register_done.html',
                          {'new_user': new_user})
    else:
        user_form = UserRegistrationForm()
    return render(request,
                  'account/register.html',
                  {'user_form': user_form})

这是学生应用程序中用于学术,校园,成就等的学生资料表格:

#models.py(学生应用程序):

class student(User):
 is_student = models.BooleanField(default=False)
    class academic(models.Model):
         student = models.OneToOneField(student, on_delete=models.CASCADE, primary_key=True)
         add_college=models.ForeignKey(college,db_index=True,on_delete=models.CASCADE)
         add_book=models.ForeignKey(book,db_index=True,on_delete=models.CASCADE)
         skills=models.CharField(max_length=30)
         favourite_subject=models.CharField(max_length=30)
         strong_sbject=models.CharField(max_length=30)
         weak_subject=models.CharField(max_length=30)
         fields_interested=models.CharField(max_length=30)
         motivation=models.CharField(max_length=30)



    class campus(models.Model):
         student=models.OneToOneField(student,on_delete=models.CASCADE,primary_key=True)
         endorsement=models.CharField(max_length=50)
         membership=models.CharField(max_length=50)

我们也为目标和成就提供了另一种形式。 所以同样,我也有几种其他用户类型的个人资料表格。

现在我的要求是,根据注册期间选择的用户类型(例如学生),然后应创建学生个人资料以及上述个人资料表格。什么是最佳方法?我无法在Internet上的任何地方找到合适的解决方案。.由于这个问题两个月以来我挠头。.任何解决方案都应该得到高度赞赏!

0 个答案:

没有答案