[英]Django how to save User Model?
Good day again, so I have a form and a user can add multiple titles.又是美好的一天,所以我有一个表单,用户可以添加多个标题。 Each submit will create a new object which includes the written title and the user who wrote it(submitted the form).
每次提交都会创建一个新的 object,其中包括书面标题和编写它的用户(提交表单)。
I tried it with this but I don't know what to add in NewTitle.objects.create(title=title, ...)我试过了,但我不知道在 NewTitle.objects.create(title=title, ...) 中添加什么
\views.py \views.py
def title_view(request):
try:
profile = request.user.newtitle
except NewTitle.DoesNotExist:
profile = NewTitle(user=request.user)
if request.method == 'POST':
form = NewTitleForm(request.POST, instance=profile)
if form.is_valid():
title = form.cleaned_data["title"]
NewTitle.objects.create(title=title) #in the () I have to add the user= aswell
return redirect('/another')
else:
form = NewTitleForm(instance=profile)
return render(request, 'test.html', {'form': form, 'profile': profile})
\models.py \模型.py
class NewTitle(models.Model):
user = models.OneToOneField(
User, default=None, null=True, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
I am working with basic User model.我正在使用基本用户 model。 Currently, a user can submit the form and a new object will create just with the given title but without the user who wrote it because I just added title=title and not user=...
目前,用户可以提交表单,新的 object 将仅使用给定的标题创建,但没有编写它的用户,因为我只是添加了 title=title 而不是 user=...
Any ideas?有任何想法吗?
If you want to update an existing NewTitle
for that user, or create a new one if no such item exists, you can .save()
the form
, so:如果您想为该用户更新现有的
NewTitle
,或者如果不存在这样的项目,则创建一个新的,您可以.save()
form
,因此:
from django.contrib.auth.decorators import login_required
@login_required
def title_view(request):
try:
profile = request.user.newtitle
except NewTitle.DoesNotExist:
profile = NewTitle(user=request.user)
if request.method == 'POST':
form = NewTitleForm(request.POST, instance=profile)
if form.is_valid():
form.save()
return redirect('/another')
else:
form = NewTitleForm(instance=profile)
return render(request, 'test.html', {'form': form, 'profile': profile})
You should also unident the return render(…)
part such that if the form fails, you rerender the form with the error messages.您还应该识别
return render(…)
部分,以便如果表单失败,您可以使用错误消息重新呈现表单。
If you want to create a new one each time, you can not do that with a OneToOneField
: a OneToOneField
is a ForeignKey
with unique=True
: it thus means that the user has at most one NewTitle
object that refers to that user.如果你想每次都创建一个新的,你不能用
OneToOneField
来做到这一点: OneToOneField
是一个具有unique=True
的ForeignKey
:这意味着用户最多有一个引用该用户的NewTitle
object。 If you want to be able to construct multiple ones, you thus can work with a ForeignKey
:如果您希望能够构建多个,则可以使用
ForeignKey
:
from django.conf import settings
from django.contrib.auth.decorators import login_required
@login_required
class NewTitle(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
default=None,
null=True,
on_delete=models.CASCADE
)
title = models.CharField(max_length=200)
and then the view looks like:然后视图看起来像:
def title_view(request):
if request.method == 'POST':
form = NewTitleForm(request.POST
, instance=NewTitle(user=request.user))
if form.is_valid():
form.save()
return redirect('/another')
else:
form = NewTitleForm()
return render(request, 'test.html', {'form': form})
but then you can of course not fetch the .newtitle
of a User
object, since there can be zero, one or multiple ones.但是您当然不能获取
User
.newtitle
的 .newtitle,因为可以有零个、一个或多个。
Note : It is normally better to make use of the
settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use theUser
model [Django-doc] directly.注意:通常使用
settings.AUTH_USER_MODEL
[Django-doc]引用用户 model 比直接使用User
model [Django-doc] 更好。 For more information you can see the referencing theUser
model section of the documentation .有关更多信息,您可以查看参考文档的
User
model部分。
Note : You can limit views to a view to authenticated users with the
@login_required
decorator [Django-doc] .注意:您可以使用
@login_required
装饰器 [Django-doc]将视图限制为经过身份验证的用户的视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.