djangosnippets.org: Latest snippets tagged with 'db'https://djangosnippets.org/tags/db/2021-03-24T04:15:55.218642-05:00Log the time taken to execute each DB query
2021-03-24T04:15:55.218642-05:00kennyx46https://djangosnippets.org/snippets/10853/<p>Use this snippet to log the time taken to execute a specific DB query.</p>
Freely redistributableByteSplitterField
2011-08-24T17:18:17.166457-05:00Lacourhttps://djangosnippets.org/snippets/2528/<p>When you want to save integers to the db, you usually have the choice between 16-, 32- and 64-bit Integers (also 8- and 24-bit for MySQL). If that doesn't fit your needs and you want to use your db-memory more efficient, this field might be handy to you.
Imagine you …</p>
Freely redistributableTinyIntegerField
2011-08-15T17:39:42.478692-05:00Lacourhttps://djangosnippets.org/snippets/2513/<p>Django lacks support of MySQL's "tinyint" 8-bit-integer datatype. This snippet gives you a TinyIntegerField and a PositiveTinyIntegerField. Falls back Django's SmallIntegerField if a different database-engine is used</p>
Freely redistributableFinding related objects for instances in a queryset
2011-03-08T05:47:58.285239-06:00akaiholahttps://djangosnippets.org/snippets/2385/<p>When deleting objects in Django's admin interface, it lists other objects which would be deleted and asks for confirmation. This snippet does the same programmatically.</p>
<p>The snippet works in Django 1.3 (more specifically, revision 14507 or later). It uses Django internals which are not a part of the public API, …</p>
Freely redistributableImproved Pickled Object Field (Fixed for Django 1.2)
2011-02-02T11:56:43.483263-06:00danielsokolowskihttps://djangosnippets.org/snippets/2346/<p>Small changes to <a href="http://djangosnippets.org/snippets/1694/">Snippet 1694</a> to that QueryAPI works for django 1.2 and higher.
</p>
<p>Changes:
</p>
<ul>
<li>
Replaced <code>get_db_prep_value</code> with <code>get_prep_value</code>.
</li>
<li>
Replaced <code>get_db_prep_lookup</code> with modified <code>get_prep_lookup</code>.
</li>
</ul>
Freely redistributableLog all interaction with user to the DB
2011-01-17T15:09:15.275328-06:00inuwashihttps://djangosnippets.org/snippets/2325/<p>Due to compliance requirements in the financials industry we needed to log every request a user made to our system, the action taken (view function) and response from the server.
</p>
<p>I found a lot of other logging solution bit most revolved around debugging and DB query logging. I needed to …</p>
Freely redistributableorm_tools
2011-01-02T17:08:15.982747-06:00limscoderhttps://djangosnippets.org/snippets/2305/<p>This module contains classes that add new behavior to Django's ORM. Classes include:
</p>
<p><strong>Session</strong>
</p>
<ul>
<li>
Forces QuerySet objects to return identical instances when objects with the same primary key are queried.
</li>
<li>
Similar to <a href="http://www.sqlalchemy.org/docs/orm/session.html">SQLAlchemy Session</a>
</li>
</ul>
<p><strong>GraphSaver</strong>
</p>
<ul>
<li>
Save entire object graphs at once.
</li>
<li>
Automatically detects object dependencies and saves them in the …</li></ul>
Freely redistributablemodel instance to sql insert statement
2010-12-24T19:15:54.268156-06:00gsoltishttps://djangosnippets.org/snippets/2299/<p>This function will take a model instance and return an insert statement for it. I use it for extracting a subset of my production data so that I can reproduce problems in a local environment. The quoting is for mysql, you may have to change it depending on your db …</p>
Freely redistributabledjango_bulk_save.py - defer saving of django models to bulk SQL commits
2010-02-08T02:52:07.731305-06:00preetkukretihttps://djangosnippets.org/snippets/1913/<p>When called, this module dynamically alters the behaviour of model.save() on a list of models so that the SQL is returned and aggregated for a bulk commit later on. This is much faster than performing bulk writing operations using the standard model.save().</p>
<p>To use, simply save the code as django_bulk_save.py …</p>
Freely redistributableExtended db cache backend with 'filter() / LIKE' support (and now scheduled cache clean!)
2010-01-07T16:28:51.210350-06:00sleepycalhttps://djangosnippets.org/snippets/1854/<p>Because the db caching doesn't support atomic operations, it was unsafe to store a list of 'keys' in a single key. So, rather than store the list, I just append each key with a specific tag, and then filter for it later. This means I don't need to worry too …</p>
Freely redistributablegrep and delete sqlite tables
2009-12-07T11:28:13.892199-06:00kifkifhttps://djangosnippets.org/snippets/1829/<p>This is a slight improvment of a previous snippet of mine:
http://www.djangosnippets.org/snippets/1776/</p>
<p>It is an interactive shell script that greps and deletes sqlite tables</p>
<p>USAGE:
./pdrop.sh myquery mydbfile</p>
Freely redistributableInteger based MoneyField
2009-09-24T23:53:31.935377-05:00Jayhttps://djangosnippets.org/snippets/1741/<p>It is supposed the aggregation on integer is fast than numeric type in database duo to how they are stored as numeric is represented as string. As money only have 2 decimal place, it can be converted to an Integer despite of its decimal point.</p>
<p>The python class decimal.Decimal also …</p>
Freely redistributableImproved Pickled Object Field
2009-08-20T13:00:49.020099-05:00taavi223https://djangosnippets.org/snippets/1694/<p><a href="http://www.djangosnippets.org/snippets/513/">Based on snippet #513 by obeattie.</a></p>
<p><strong>Update 10/10/09:</strong> <a href="http://github.com/shrubberysoft/django-picklefield">Further development is now occurring on GitHub, thanks to Shrubbery Software.</a></p>
<p>Incredibly useful for storing just about anything in the database (provided it is Pickle-able, of course) when there isn't a 'proper' field for the job.</p>
<p><code>PickledObjectField</code> is database-agnostic, and should work …</p>
Freely redistributableJSONField
2009-05-02T22:44:30.260569-05:00Jasberhttps://djangosnippets.org/snippets/1478/<p>This is a custom field that lets you easily store JSON data in one of your model fields. This is updated to work with Django 1.1.</p>
<p><strong>Example: (models.py)</strong></p>
<pre><code>from django.db import models
import JSONField
class MyModel(models.Model):
info = JSONField()
</code></pre>
<p><strong> Example: (shell)</strong></p>
<pre><code>>>> obj = MyModel.objects.all()[0]
>>> type(obj.info)
<type 'NoneType'>
>>> …</code></pre>
Freely redistributableiter_fetchmany
2009-01-08T02:13:06.075286-06:00jdunckhttps://djangosnippets.org/snippets/1280/<p>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.)</p>
<p>This snippet, hoisted from django.db.models.sql.query.results_iter, presents a nice, …</p>
Freely redistributable