简体   繁体   English

django模型方法中的反向查找

[英]django reverse lookup in model method

Google has lots of examples of doing reverse lookup in the interactive prompt but none of doing them inside a django model as a method. Google有很多在交互式提示中进行反向查找的示例,但都没有在Django模型中作为方法来进行反向查找的示例。

I have the following models.py file: 我有以下models.py文件:

class Client(models.Model):
    ...

    def __unicode__(self):
        return ???

class ClientDetails(models.Model):
    client = models.ForeignKey(Client, null=True)
    created = models.DateTimeField(default=datetime.now)
    created_by = models.ForeignKey(User, null=True)
    name_title = models.CharField(max_length=3, choices=NAME_TITLE_CHOICES)
    first_name = models.CharField(max_length=40)
    middle_name = models.CharField(max_length=40)
    last_name = models.CharField(max_length=40)
    ...

How do I get the client method to return last_name from ClientDetails? 我如何获得从ClientDetails返回last_name的客户端方法?

If a ClientDetails object should only ever be associated with a single Client object, then I would change your FK to a OneToOneField, which will provide you with a neat reverse accessor that can only ever link between a given Client and its associated ClientDetails. 如果ClientDetails对象仅应与单个Client对象相关联,则可以将FK更改为OneToOneField,这将为您提供一个纯净的反向访问器,该访问器只能在给定Client及其关联的ClientDetails之间进行链接。 Then you can do: 然后,您可以执行以下操作:

try:
   return self.clientdetails.last_name
except ClientDetails.DoesNotExist:
   ##handle it here, returning a graceful message

Alternatively, if you kept it as a FK, then you'd have to do something like: 或者,如果将其保留为FK,则必须执行以下操作:

try:
    self.clientdetails_set.all()[0].last_name
except IndexError, e:
    ## handle exception here

but using a FK here is brittle and not great form (as the exception-handling implies: if there is none returned, then you'll get an IndexError. Also, there might be more than one ClientDetails object linked to that Client, and you'd only get the details of the first one here.) 但是在这里使用FK是脆弱的,并且不是很好的形式(如异常处理所暗示的:如果未返回任何内容,则将收到IndexError。此外,可能有多个ClientDetails对象链接到该Client,并且您只会在这里获得第一个的详细信息。)

So, I really would recommend using a OneToOneField instead of that FK. 因此,我真的建议使用OneToOneField而不是FK。 (All a OneToOneField is basically an FK with unique=True set on it and some neater accessors than standard FKs) (所有的OneToOneField基本上都是一个FK,上面设置了unique=True ,并且访问器比标准FK更整洁)

For that you can use in client the *clientdetails_set* to access all the ClientDetails objects that are linked to that client. 为此,您可以在客户端中使用* clientdetails_set *来访问链接到该客户端的所有ClientDetails对象。

The set is an object set from Django, so calling the method all() will retrieve every of the objects. 该集合是Django的对象集,因此调用方法all()将检索每个对象。 If you know there's only one you can do self.clientdetails_set.all()[0].last_name 如果您知道只有一个,则可以进行self.clientdetails_set.all()[0] .last_name

Here's the link for the django documentation: Link 这是django文档的链接链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM