反向查找链接到配置文件模型的表,该配置文件模型链接到用户模型

发布于 2024-11-19 11:54:39 字数 885 浏览 4 评论 0原文

我正在使用 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 particular Company, it should be the company to which that Personnel belongs to.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青萝楚歌 2024-11-26 11:54:39

您应该能够通过 request.user.get_profile().company 访问该公司。请参阅 django 文档

您无法直接在表单中访问 request,因为表单也可以在其他地方实例化,而不仅仅是在不存在 request 对象的视图中。您可以在初始化表单时将当前用户传递给表单(请记住,传递用户而不是请求更有意义,因此您可以在没有请求的情况下使用它出色地)。

class MyForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        super(MyForm, self).__init__(*args, **kwargs)

# in you view
form = MyForm(user=request.user)

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 no request 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).

class MyForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        super(MyForm, self).__init__(*args, **kwargs)

# in you view
form = MyForm(user=request.user)
俯瞰星空 2024-11-26 11:54:39

从用户到公司没有直接链接,因此您需要执行 request.user.personnel.company

您始终需要显式传递请求。您可以在初始化时将其存储在表单中并稍后访问它:

class MyForm(forms.ModelForm):
  ... fields, meta, etc ...
  def __init__(self, *args, **kwargs):
    self.request = kwargs.pop('request', None)
    super(MyForm, self).__init__(*args, **kwargs)

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:

class MyForm(forms.ModelForm):
  ... fields, meta, etc ...
  def __init__(self, *args, **kwargs):
    self.request = kwargs.pop('request', None)
    super(MyForm, self).__init__(*args, **kwargs)
零度℉ 2024-11-26 11:54:39

对于 Company 模型实例,您可以使用以下方法获取所有 Personnel(以供将来参考,django 模型名称应该是单数):

company = Company.objects.get(id=some_company_id)
personnel = company.personnel_set.all()

如果您有特定的 Personnel 实例:

person = request.user.get_profile()
company = person.company

对于您的表单。如果您的表单需要访问当前用户,那么您需要将其传递给它。您可以通过覆盖表单上的 init 函数来完成此操作

class SomeForm(forms.Form):
    def __init__(self, user, *args, **kwargs):
         super(self, SomeForm).__init__(*args, **kwargs)
         self.user = user

For Company model instances you can get all Personnel (for future reference in django models names should be singular) by using:

company = Company.objects.get(id=some_company_id)
personnel = company.personnel_set.all()

If you have a particular Personnel instance:

person = request.user.get_profile()
company = person.company

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

class SomeForm(forms.Form):
    def __init__(self, user, *args, **kwargs):
         super(self, SomeForm).__init__(*args, **kwargs)
         self.user = user
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文