HackerTrans
TopNewTrendsCommentsPastAskShowJobs

Suor

no profile record

Submissions

[untitled]

1 points·by Suor·3 года назад·0 comments

[untitled]

1 points·by Suor·3 года назад·0 comments

[untitled]

1 points·by Suor·3 года назад·0 comments

Ban 1+N in Django

suor.github.io
175 points·by Suor·3 года назад·148 comments

comments

Suor
·3 года назад·discuss
Writing things directly is tightly coupling code. Sometimes quite distant portions of it, i.e. database access and presentation logic. With ORM and some smarter lazy technics or introspection at least you can untie it, with hand written SQL there is no way. And the reason is strings are poorly composable. Unless you use some query generator, but then we are back to ORM-like something.
Suor
·3 года назад·discuss
Never used this, but I see that it provides a context manager/decorator, which you can use to "shield" any code. And also some shortcuts for DjangoREST or something.

Regarding extra code, you can simply replace the Djangos' `render()`, with the one from this lib and you are done. I would have probably made a custom template extension or something to do that for all renders automatically though too.
Suor
·3 года назад·discuss
cacheops is more of a general purpose ORM caching with automatic invalidation.
Suor
·3 года назад·discuss
For that fetching in chunks is usually a way to go. I use something of these usually https://handy.readthedocs.io/en/latest/db.html#queryset_iter...
Suor
·3 года назад·discuss
GraphQL makes it even more fun :)
Suor
·3 года назад·discuss
I usually use `.values()` and `.values_list()` only when model creation is really slow. Usually many rows fetched.
Suor
·3 года назад·discuss
Actually there is such thing already https://pypi.org/project/django-auto-prefetch/ Not a monkey patch though, so will only work on the models you inherit from this.
Suor
·3 года назад·discuss
Here is an example of such thing https://github.com/Suor/django-cacheops/blob/8b3a79de29b2545...
Suor
·3 года назад·discuss
Should be easy enough to implement. You only need a context manager that adds 1 to some threadlocal flag on enter and subrracts on exit then check this flag in the monkey patch. Not sure how costly that will be though.
Suor
·3 года назад·discuss
cacheops can do the same, but it never came to my mind to actually go this way. I would rather write some special code to fetch with `id__in=[...]` and then cache individually.
Suor
·3 года назад·discuss
HN keeps automatically replacing the URL. It used to be a redirect before, but not anymore
Suor
·3 года назад·discuss
Thanks