反向查找链接到配置文件模型的表,该配置文件模型链接到用户模型
我正在使用 django.contrib.auth 进行身份验证。 User
模式有一个名为 Personnel
的自定义配置文件模型。 Personnel
链接到另一个名为 Company
的表。
class Personnel(models.Model):
"""
Model for storing the personnel information
"""
user = models.OneToOneField(
User
)
company = models.ForeignKey(
Company, null=False, verbose_name="Company"
)
class Company(models.Model):
"""
Model for storing the company information.
"""
company_name = models.CharField(
null=False, max_length=200, verbose_name="Company Name"
)
用户通过身份验证后。我如何获取用户的公司? 类似
的内容request.user....
在视图中,我可以访问请求,但如果我需要访问表单和模型中的
request
变量,我是否需要将请求变量传递给表单/模型,或者有什么方法可以访问它?这是因为当我存储特定Company
的信息时,它应该是该Personnel
所属的公司。
I'm uisng django.contrib.auth
for authentication. The User
mode has a custom profile model called Personnel
. Personnel
is linked to another table called Company
.
class Personnel(models.Model):
"""
Model for storing the personnel information
"""
user = models.OneToOneField(
User
)
company = models.ForeignKey(
Company, null=False, verbose_name="Company"
)
class Company(models.Model):
"""
Model for storing the company information.
"""
company_name = models.CharField(
null=False, max_length=200, verbose_name="Company Name"
)
Once the user is authenticated. How can i fetch the company for a user? Something like
request.user....
In the view, I can access the request but if I need to access the
request
variable in my form and my model, do I need to pass the request variable to the form/model or is there any way to access it? This is because when I'm storing information for a particularCompany
, it should be the company to which thatPersonnel
belongs to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该能够通过
request.user.get_profile().company
访问该公司。请参阅 django 文档 。您无法直接在表单中访问
request
,因为表单也可以在其他地方实例化,而不仅仅是在不存在request
对象的视图中。您可以在初始化表单时将当前用户传递给表单(请记住,传递用户而不是请求更有意义,因此您可以在没有请求的情况下使用它出色地)。You should be able to access the company through
request.user.get_profile().company
. See the django documentation.You cannot access
request
directly in a form, as the form could instantiated somewhere else too, not only in a view where norequest
object exists. You could pass the current user to the form when you initialize it (remember it makes more sense to pass the user not the request, so you can use it without a request as well).从用户到公司没有直接链接,因此您需要执行
request.user.personnel.company
。您始终需要显式传递
请求
。您可以在初始化时将其存储在表单中并稍后访问它:There's no direct link from User to Company, so you need to do
request.user.personnel.company
.You'll always need to pass around the
request
explicitly. You can store it inside the form on initialization and access it later:对于 Company 模型实例,您可以使用以下方法获取所有 Personnel(以供将来参考,django 模型名称应该是单数):
如果您有特定的 Personnel 实例:
对于您的表单。如果您的表单需要访问当前用户,那么您需要将其传递给它。您可以通过覆盖表单上的 init 函数来完成此操作
For Company model instances you can get all Personnel (for future reference in django models names should be singular) by using:
If you have a particular Personnel instance:
For your form. If your form needs access to the current User then you will need to pass it to it. You do this by overriding the init function on the form