django/python - db hits while accessing variables -
class appuser(models.model): user = models.foreignkey(user, unique=true) slug = models.slugfield(editable=false, blank=true)
for above model suppose have object "appuser" have rendered in template
does django hits user db each time when use appuser.user.username in our code??
any on how in django/python db lookups done while accessing foreign key , many-to-many variables ??
you should read through details database access optimization: https://docs.djangoproject.com/en/1.3/topics/db/optimization/
and select_related()
:
https://docs.djangoproject.com/en/dev/ref/models/querysets/#select-related
as quick answer question: depends on how you've retrieved object
with
appuser = appuser.objects.get(pk=id)
it hit database separately appuser.user.
but with
appuser = appuser.objects.select_related().get(pk=id)
it joined query, accessing appuser.user not trigger sql query.
Comments
Post a Comment