简体   繁体   中英

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). 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 . Is this three variables ( tr_rowid_debtor , de_listed_date and lte ) or is this some special construct for Python? It is occurring in a statement that builds a query string for 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. In the case of tr_rowid_debtor__de_listed_date__lte , three things are happening.

  1. tr_rowid_debtor specifies the attribute on DeTransaction , which is a relationship based on what happens next
  2. de_listed_date specifies the field of the related model to query against
  3. lte says to perform the comparison using <= (less than or equal to)

It's worth a read of Django's query docs . The cover these in some detail.

As for whether or not this is special to Python, it is not. the __ is assigned to a constant called LOOKUP_SEP . The value is used with str.split() to generate the WHERE clause for queries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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