6

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.

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
Gary Ridley
  • 317
  • 1
  • 4
  • 8

1 Answers1

12

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.

dirn
  • 19,454
  • 5
  • 69
  • 74
  • Thanks to all for the answers, this will help. Reading Django query docs now. Is there a good recent book/ebook on Django/Python? – Gary Ridley Jan 23 '14 at 23:31