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.
tr_rowid_debtor specifies the attribute on DeTransaction, which is a relationship based on what happens nextde_listed_date specifies the field of the related model to query againstlte 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With