django - Using `defer` or `only` on ForeignKey -


i'm trying squeeze few milliseconds view. i'd avoid loading pretty large text fields foreignkey model won't use.

to make clearer:

class foo(models.model):     slug = models.slugfield()     text1 = models.textfield()     text2 = models.textfield()     ...      @models.permalink     def get_absolute_url(self):         return ('foo_detail', (), {"object_id": self.pk, "object_slug": self.slug})   class bar(models.model):     foo = models.foreignkey(foo)     ... 

then in template:

 {% bar in bars %}       {{ bar.foo.get_absolute_url }}  {% endfor %} 

now, if @ queries issued orm, each foo django retrieves every field (as expected), in end pk , slug needed. in case, sums more or less hundredth of second per object on testing machine.

of course write method this:

class bar(models.model):     ...     def get_foo_absolute_url(self):         return foo.objects.only('pk', 'slug').get(pk=self.foo).get_absolute_url() 

and use in template, it's ugly hell.

did face same problem , come better solution?

we had similar issue, moved solution - cached url in memcache/state cache or precalculated in model field.

we tested out reverse of url, models.permalink does, lowering performance.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -