Login

Tag "db-api"

Snippet List

iter_fetchmany

When executing custom sql, the temptation is to use fetchall or fetchone, since the API for fetchmany is a bit awkward. (fetchall makes all records resident in client memory at once; fetchone takes a network round-trip to the DB for each record.) This snippet, hoisted from django.db.models.sql.query.results_iter, presents a nice, simple iterator over multiple fetchmany calls which hits a sweet spot of minimizing memory and network usage.

  • sql
  • db
  • db-api
  • fetchmany
Read More

Function/Stored Procedure Manager

Ever want to call stored procedures from Django easily? How about PostgreSQL functions? That's that this manager attempts to help you with. To use, just stick this in some module and in a model do: class Article(models.Model): objects = ProcedureManager() Now you can call procedures (MySQL or PostgreSQL only) to filter for models like: Article.objects.filter_by_procedure('ProcName', request.user) This will attempt to construct a queryset of objects. To just get random values, do: Article.objects.values_from_procedure('ProcName', request.user) Which will give you a list of dictionaries.

  • function
  • db
  • manager
  • db-api
  • stored-procedure
Read More

3 snippets posted so far.