Saturday, 15 March 2014

python - Django query single underscore behaving like double underscore? -



python - Django query single underscore behaving like double underscore? -

i made typo in code , noticed got same behavior, wondering difference between single , double underscores in django queries.

>>> underscore = mymodel.objects.filter(foreign_key_id=var) >>> double_underscore = mymodel.objects.filter(foreign_key__id=var) >>> underscore == double_underscore false >>> list(underscore) == list(double_underscore) true

i'm not sure equality method beingness used compare querysets, when convert python lists find same elements contained within. have insight what's going on here?

those 2 fields happen both exist.

foreign_key_id automatically created column on mymodel object, whereas foreign_key__id id on foreign key table itself.

these values both same..

mymodel1.foreign_key_id == 5 # stored on model # , not require lookup. mymodel1.foreign_key.id == 5 # stored on target table # , requires db hit.

python mysql django django-models django-queryset

No comments:

Post a Comment