Django:具有其他字段的CreateView? [英] Django: CreateView with additional field?
问题描述
我正在尝试编写Django CreateView(CBV),它使用用户电子邮件代替用户ID,并根据电子邮件确定(或创建)用户。
I am trying to program a Django CreateView (CBV), which takes instead of the user id the user email and determines (or creates) the user based on the email.
我的模型不包含任何特殊内容:
My model does not contain anything special:
class Project(models.Model):
name = models.CharField(_('Title'), max_length=100,)
user = models.ForeignKey(User, verbose_name=_('user'),)
...
我的forms.py在表单中添加了其他电子邮件字段:
My forms.py adds the additional email field to the form:
class ProjectCreateForm(forms.ModelForm):
email = forms.EmailField(required=True, )
class Meta:
model = Project
fields = ('name', ...,)
在我看来。 py,我正在尝试确定用户是否存在或应该创建该用户。在这两种情况下,都应将用户ID保存为Project实例的一部分。
In my views.py, I am trying to determine if the user exists or should be created. In both cases, the user id should be saved as part of the Project instance.
class ProjectCreateDetails(CreateView):
form_class = ProjectCreateForm
template_name = '...'
success_url = reverse_lazy('login')
model = Project
def form_valid(self, form):
try:
user = User.objects.get(email=form.email)
except User.DoesNotExist:
user = User.objects.create_user(form.email, form.email, ''.join([random.choice(string.digits + string.letters) for i in range(0, 10)]))
user.save()
form.instance.user = user
return super(ProjectCreateDetails, self).form_valid(form)
但是我遇到一个错误,即'解决方案'对象没有属性'电子邮件'
。
However I am facing an error that the 'Solution' object has no attribute 'email'
.
是否需要切换到FormView而非CreateView?
Do I need to switch to a FormView instead of a CreateView?
推荐答案
您收到错误'Solution'对象没有属性'email'
,因为 form.email
无效。验证数据永远不能用作表单或模型表单的属性。当表单(包括模型表单)有效时, form.cleaned_data
字典。
You get the error 'Solution' object has no attribute 'email'
because form.email
is invalid. Validated data is never available as attributes of a form or model form. When forms (including model forms) are valid, the successfully validated data is available in the form.cleaned_data
dictionary.
请注意,不需要调用 user.save()
。 create_user
调用已将用户添加到数据库中。您也不必生成随机密码-如果密码
为 None
,则 create_user
将设置无法使用的密码。
Note that you don't need to call user.save()
. The create_user
call has already added the user to the database. You don't have to generate a random password either -- if password
is None
, then create_user
will set an unusable password.
最后,请确保您不包括 user $ c $
ProjectCreateForm
中的c>字段。您可能没有,但是您的代码显示 fields =('name',...,)
,所以我不确定。
Finally, make sure that you do not include the user
field in the ProjectCreateForm
. You probably do not, but your code says fields = ('name', ...,)
so I can't tell for sure.
将其放在一起,您将获得以下(未经测试的)代码:
Put it together and you get the following (untested) code:
def form_valid(self, form):
try:
user = User.objects.get(email=form.cleaned_data['email'])
except User.DoesNotExist:
user = User.objects.create_user(form.cleaned_data['email'], form.cleaned_data['email'])
form.instance.user = user
return super(ProjectCreateDetails, self).form_valid(form)
这篇关于Django:具有其他字段的CreateView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!