简体   繁体   English

双下划线表示什么?

[英]What do double underscores indicate?

In Python I know that double underscores preceding a name cause Python to prepend the classname to the variable name as in _classname__variable name (name mangling). 在Python中,我知道在名称前加双下划线会导致Python在classname名之前加variable名,就像_classname__variable name(名称_classname__variable )一样。 So my question is this, in an Python/Django app I have been asked to modify, there are variable(?) names of the type tr_rowid_debtor__de_listed_date__lte . 所以我的问题是,在我被要求修改的Python / Django应用程序中,存在类型为tr_rowid_debtor__de_listed_date__lte变量(?)名称。 Is this three variables ( tr_rowid_debtor , de_listed_date and lte ) or is this some special construct for Python? 这是三个变量( tr_rowid_debtorde_listed_datelte )还是这是Python的一些特殊构造? It is occurring in a statement that builds a query string for Django ... 它发生在为Django构建查询字符串的语句中...

query = DeTransaction.objects.select_related().filter(
    tr_rowid_debtor__de_listed_date__lte=to_date,
    tr_rowid_debtor__de_rowid_client__cl_rowid=in_client
).values(
    'tr_rowid_debtor','tr_rowid_debtor__de_listed_date',
    'tr_payment_date','tr_account','tr_to_agency','tr_to_client'
)

Any advice here would be appreciated. 这里的任何建议将不胜感激。

The double underscore notation is used by Django's ORM to indicate some sort of separation in a query. Django的ORM使用双下划线符号表示查询中的某种分隔。 In the case of tr_rowid_debtor__de_listed_date__lte , three things are happening. 对于tr_rowid_debtor__de_listed_date__lte ,正在发生三件事。

  1. tr_rowid_debtor specifies the attribute on DeTransaction , which is a relationship based on what happens next tr_rowid_debtor指定的属性DeTransaction这是基于接下来会发生什么关系,
  2. de_listed_date specifies the field of the related model to query against de_listed_date指定要查询的相关模型的字段
  3. lte says to perform the comparison using <= (less than or equal to) lte表示使用<= (小于或等于)执行比较

It's worth a read of Django's query docs . 值得阅读Django的查询文档 The cover these in some detail. 详细介绍这些内容。

As for whether or not this is special to Python, it is not. 至于这是否对Python来说很特殊,不是。 the __ is assigned to a constant called LOOKUP_SEP . __被分配给一个名为LOOKUP_SEP的常量。 The value is used with str.split() to generate the WHERE clause for queries. 该值与str.split()一起使用以生成查询的WHERE子句。

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

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