排除通用 CRUD 视图中的字段
我有一个名为 Domain
的模型,如下所示:
class Domain(models.Model):
"""
Model for storing the company domains
"""
user = models.ForeignKey(
User
)
host = models.CharField(
null=False, verbose_name="Host", max_length=128, unique=True
)
我想使用 Django 的通用视图对此进行 CRUD 操作。该模型中有一个字段需要用户输入,但外键字段不需要任何用户输入。如何从通用视图生成的表单中排除该字段,但为其分配当前经过身份验证的用户的值。
谢谢。
I have a model named Domain
which looks like this:
class Domain(models.Model):
"""
Model for storing the company domains
"""
user = models.ForeignKey(
User
)
host = models.CharField(
null=False, verbose_name="Host", max_length=128, unique=True
)
I'd like to use Django's generic views for doing CRUD operations on this. There is one field in this model that needs user input but the foreign key field doesn't need any user input. How can I exclude that field from the form that my generic view generates but assign it the value of the current authenticated user.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看 Russel 在 django-users 上对类似问题的回答本周早些时候小组。
引用答案*:
*原始答案设置
self.object.user
而不是form.instance.user
。这会产生一个 AttributeError ,所以我在上面更改了它。Have a look at Russel's answer to a similar question on the django-users group earlier this week.
Quoting the answer*:
*the original answer set
self.object.user
instead ofform.instance.user
. This gives anAttributeError
so I have changed it above.