首页 > 解决方案 > Django创建用户配置文件

问题描述

我为我的系统创建了一个用户配置文件模型。我创建了所有模型,并且效果很好。我有一个表格,表格也有效。但是当我从管理页面查看用户创建表单时,它看起来不一样。

有一些缺失的部分,如等级,comp_name。我该如何解决?

模型.py

class UserProfile(models.Model):
    ranks = (
        ('xxx', 'xxx'),
          ...
    
    )
    comp_name = models.CharField(max_length=200, default="Choose")
    user_id = models.UUIDField(default=uuid.uuid4(), editable=False, unique=True)
    username = models.CharField(max_length=500)
    first_name = models.CharField(max_length=200, default=None)
    last_name = models.CharField(max_length=200, default=None)
    password = models.CharField(max_length=50)
    email = models.EmailField(max_length=254)
    rank = models.CharField(max_length=200, choices=ranks)

表格.py

class SignUpForm(UserCreationForm):
    comp_name = forms.CharField(label='What is your company name?')
    email = forms.CharField(max_length=254)
    rank = forms.ChoiceField(label='What is your rank?', choices=UserProfile.ranks)
    first_name = forms.CharField(max_length=250)
    last_name = forms.CharField(max_length=250)
    comp_name = forms.ModelChoiceField(queryset=CompanyProfile.objects.all())
    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email', 'comp_name',  'password1', 'password2', 'rank'

admin 管理面板“更改用户”屏幕

标签: pythondjangodjango-modelsdjango-admin

解决方案


在forms.py

class SignUpForm(UserCreationForm):
    comp_name = forms.CharField(label='What is your company name?')
    email = forms.CharField(max_length=254)
    rank = forms.ChoiceField(label='What is your rank?', choices=UserProfile.ranks)
    first_name = forms.CharField(max_length=250)
    last_name = forms.CharField(max_length=250)
    comp_name = forms.ModelChoiceField(queryset=CompanyProfile.objects.all())
    class Meta:
        model = User --> change to UserProfile
        fields = ('username', 'first_name', 'last_name', 'email', 'comp_name',  'password1', 'password2', 'rank'

推荐阅读