我扩展了 django 中的用户模型以包含其他几个变量,例如位置和雇主。现在我正在尝试创建一个具有以下字段的表单:
First name (from User)
Last name (from User)
Location (from UserProfile, which extends User via a foreign key)
Employer (also from UserProfile)
我创建了一个模型:
from django.forms import ModelForm
from django.contrib import auth
from alert.userHandling.models import UserProfile
class ProfileForm(ModelForm):
class Meta:
# model = auth.models.User # this gives me the User fields
model = UserProfile # this gives me the UserProfile fields
所以,我的问题是,如何创建一个可以访问所有字段的 ModelForm,无论它们是来自 User 模型还是 UserProfile 模型?
希望这是有道理的。如果有任何问题,我很乐意澄清。
您可以创建两个模型表单(一个用于用户,一个用于用户配置文件),也可以创建一个包含所有字段的自定义表单并将其分派到您的视图中。
from django import forms
class ContactForm(forms.Form):
first_name = forms.CharField()
last_name = forms.CharField()
location = forms.CharField()
employer = forms.CharField()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)