【问题标题】:Django create user profileDjango创建用户配置文件
【发布时间】:2020-12-23 09:41:07
【问题描述】:

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

缺少一些部分,例如排名,comp_name。我该如何解决?

models.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)

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
        fields = ('username', 'first_name', 'last_name', 'email', 'comp_name',  'password1', 'password2', 'rank'

管理员 admin panel "Change User" screen

【问题讨论】:

标签: python django django-models django-admin


【解决方案1】:

在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'

【讨论】:

  • 是的。我迁移它
猜你喜欢
  • 2012-07-14
  • 2019-05-23
  • 2020-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-05
  • 2020-08-18
相关资源
最近更新 更多