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
Post a Comment