Django,类视图:如何使用表单对象保存会话数据?
我试图将当前请求会话中的用户名存储到数据库对象中。我怎样才能从基于类的视图中做到这一点?有没有一种“干净”的方法来做到这一点?我应该重写/子类化什么?
我有一个如下所示的模型:
from django.contrib.auth.models import User
class Entry(django.db.models.Model):
...
author = models.ForeignKey(User, editable=False)
我还有一个基于内置通用视图 django.views.generic.CreateView 的视图。我还使用模型附带的默认 ModelForm 类,以及模板中的默认 {{ form }}
。 AFAIK,会话和身份验证应用程序/中间件已正确设置——按照新 Django 项目中的默认设置。
我发现这个 post,它得到了几乎相同的东西,但是从错误的角度,并使用函数视图代替。
到目前为止,我的想法是覆盖表单类中的某些内容并将用户名插入到清理后的数据中。有更好的办法吗?有正确的方法吗?
编辑:到目前为止,解决方案不起作用,带有 IntegrityError:author_id 不能为空
from django.views.generic import CreateView
class Index(CreateView):
model = magicModel
template_name = "index.html"
success_url = "/magicWorked"
...
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.author = request.user
return super(Index, self).form_valid(form)
我根据在 django/views/generic/edit.py 中找到的内容编写了此内容,它使用类 ModelFormMixin 的实现:
def form_valid(self, form):
self.object = form.save()
return super(ModelFormMixin, self).form_valid(form)
这是名为的方法通过上面的 super().form_valid() 。
编辑: 我的解决方案的问题在于我对 Python 继承模型的理解。当超类调用 form_valid() 时,它调用的是它自己的版本,而不是我的覆盖版本;我的代码根本没有运行过。
I'm trying to store the username from the current request's session into a db object. How can I do this from within a class-based view? Is there a "clean" way to do this? What should I override/subclass?
I have a model that looks like this:
from django.contrib.auth.models import User
class Entry(django.db.models.Model):
...
author = models.ForeignKey(User, editable=False)
I also have a view based on the built-in generic view django.views.generic.CreateView
. I'm also using the default ModelForm class that goes with my model, and the default {{ form }}
in my template. AFAIK, the session and authentication apps/middleware are set up properly---as per default in new Django projects.
I found this post, which is getting at about the same thing, but from the wrong angle, and using function views instead.
My thinking so far was to override something in the form class and insert the username into the cleaned data. Is there a better way? Is there a right way?
Edit: Solution so far, non-working, with an IntegrityError: author_id cannot be null
from django.views.generic import CreateView
class Index(CreateView):
model = magicModel
template_name = "index.html"
success_url = "/magicWorked"
...
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.author = request.user
return super(Index, self).form_valid(form)
I wrote this based on what I found in django/views/generic/edit.py, which uses this implementation for class ModelFormMixin:
def form_valid(self, form):
self.object = form.save()
return super(ModelFormMixin, self).form_valid(form)
This is the method called by super().form_valid() above.
Edit: The problem with my solution was my understanding of Python's inheritance model. When the super-class calls form_valid(), it calls its own version, not my override; my code was never running at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果通用视图不够用,“正确”的方法是编写自己的视图来创建对象。创建视图相对较短,并且有很多如何保存外键的示例。
The "correct" way to do this is to write your own view for object creation if the generic view doesn't suffice. Creation views are relatively short and there are numerous examples of how to save foreign keys.
顺便说一句,Django 1.3 文档在某处说正在“讨论”对管理应用程序使用的身份验证模型的修改,例如添加每个实例的权限。 (当前的身份验证模型仅支持每个模型的权限。)开发人员还可能为我想要实现的目标添加一个实现。毕竟,几乎所有网站都使用与用户相关的数据。
Incidentally, Django's 1.3 docs say somewhere in there that modifications to the authentication model used by the admin app are being "discussed," such as adding per-instance permissions. (The current auth model supports only per model permissions.) The dev's might also add an implementation for what I'm trying to achieve. After all, user-associated data is used by nearly all websites.