gpt4 book ai didi

python - 如何更改字段的顺序?

转载 作者:太空宇宙 更新时间:2023-11-03 18:24:46 25 4
gpt4 key购买 nike

请帮助更改屏幕上表单字段的顺序。

我创建了以下模型:

class UserProfile(User):
status = models.CharField(
max_length=30,
blank=True,
)

address = models.EmailField(
max_length=50,
blank=True,
)

objects = UserManager()

然后将此模型的字段与注册表单的字段组合起来:

class MyRegistrationForm(UserCreationForm): 
username = forms.CharField(
label='username',
help_text='',
required=True,
)

password1 = forms.CharField(
label='pass1',
help_text='',
required=True,
)

password2 = forms.CharField(
label='pass2',
help_text='',
required=True,
)

class Meta:
model = UserProfile
fields = ('status', 'address')

结果用户在显示字段上看到的顺序如下:状态、地址、用户名、pass1、pass2

但我需要用户按以下顺序查看屏幕字段:用户名、地址、pass1、pass2、状态

最佳答案

根据documentation :

The generated Form class will have a form field for every model field specified, in the order specified in the fields attribute.

class MyRegistrationForm(UserCreationForm): 
...
class Meta:
model = UserProfile
fields = ('username', 'address', 'password1', 'password2', 'status')

另请参阅:How can I order fields in Django ModelForm?

关于python - 如何更改字段的顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23411467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com