djangosnippets.org: Latest snippets tagged with 'database'https://djangosnippets.org/tags/database/2021-03-18T19:34:06.513820-05:00django database snippet
2021-03-18T19:34:06.513820-05:00ItsRLuohttps://djangosnippets.org/snippets/10820/<p>..</p>
Freely redistributableFriendly ID(Python 3.X)
2017-07-06T04:48:10.879771-05:00rana-ahmedhttps://djangosnippets.org/snippets/10613/<p>This is just modified version of <a href="https://djangosnippets.org/snippets/1249/">friendly id</a> for make this script compatible with python 3.x</p>
<p>Invoice numbers like "0000004" are a little unprofessional in that they expose how many sales a system has made, and can be used to monitor the rate of sales over a given time. They …</p>
Freely redistributableDjango chunked queryset iterator
2017-01-09T08:59:51.123659-06:00mingdongthttps://djangosnippets.org/snippets/10599/<p>The function slices a queryset into smaller querysets containing chunk_size objects and then yield them.
It is used to avoid memory error when processing huge queryset, and also database error due to that the database pulls whole table at once.
Concurrent database modification wouldn't make some entries repeated or skipped …</p>
Freely redistributableTesting for pending migrations in Django
2016-05-03T12:05:40.084157-05:00dlespiauhttps://djangosnippets.org/snippets/10567/<p>DB migration support has been added in Django 1.7+, superseding South. More specifically, it's possible to automatically generate migrations steps when one or more changes in the application models are detected. Definitely a nice feature!</p>
<p>I've written a small generic unit-test that one should be able to drop into the …</p>
Freely redistributableMulti-DB Reconnecting Persistent Postgres Connection
2013-06-28T01:48:51.902847-05:00cronosahttps://djangosnippets.org/snippets/2965/<p>This is a modification of http://djangosnippets.org/snippets/1707/ that handles the database going down or PG Bouncer killing the connection. This also works in things like Twisted to make sure the connection is alive before doing a real query. Thanks @mike_tk for the original post!</p>
<p>EDIT: Updated the wrapper to handle multi-db. …</p>
Freely redistributableApplication Name DatabaseRouter
2012-10-08T15:51:06.164235-05:00kraizhttps://djangosnippets.org/snippets/2831/<p>Routes models of each app into the database with same name falling back to "default" database.</p>
Freely redistributableDatabase backup with admin command
2012-08-19T05:59:45.606089-05:00jhghttps://djangosnippets.org/snippets/2804/<p>Detect type of database (MySQL, PostgreSQL or SQLite) and make backup. In this moment ONLY WORK in GNU/Linux, NOT WIN.</p>
Freely redistributableValidation for full e-mails (e.g. "Joe Hacker <joe@hacker.com>")
2011-12-21T02:43:32.024699-06:00akaiholahttps://djangosnippets.org/snippets/2635/<p>Out of the box, Django e-mail fields for both database models and forms only accept plain e-mail addresses. For example, <code>joe@hacker.com</code> is accepted.</p>
<p>On the other hand, full e-mail addresses which include a human-readable name, for example the following address fails validation in Django:</p>
<pre><code>Joe Hacker <joe@hacker.com>
</code></pre>
<p>This package adds …</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 redistributableDecoupling models with cross-database relations
2011-02-08T01:11:16.144981-06:00zvikicohttps://djangosnippets.org/snippets/2353/<p>The snippet enables decoupling model classes, associated with a ForeignKey, for the purpose of separating them into two databases. Looking at the following example:
</p>
<pre><code>class Reporter(models.Model):
...
class Article(models.Model):
reporter = models.ForeignKey(Reporter)
</code></pre><p>We want to separate the <code>Reporter</code> and <code>Article</code> into two separate databases, but this won't work in Django …</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 redistributableJsonObjectField
2010-12-16T11:55:12.210182-06:00wattsmartinhttps://djangosnippets.org/snippets/2294/<p>This fields.py file defines a new model field type, "JsonObjectField," which is designed to allow the storage of arbitrary Python objects in Django TextFields. It is intended primarily to allow the storage of Python dictionaries or list objects. As the name implies, it converts objects to JSON for storage; this …</p>
Freely redistributablecheck database interity
2010-06-22T09:19:08.802351-05:00d2https://djangosnippets.org/snippets/2076/<p>Check database constraints after dirty import
</p>
Freely redistributableDjango Row Level Locking (Prevents race conditions if used correctly)
2010-05-26T15:58:40.424111-05:00sleepycalhttps://djangosnippets.org/snippets/2039/<p><em>WARNING</em> This is <em>extremely</em> slow.
</p>
<p>This snippet allows you to easily prevent <em>most</em> race conditions (if used properly).
</p>
<p>Feel free to extend on top of this as you like, I'd appreciate any comments to cal@simplicitymedialtd.co.uk
</p>
Freely redistributableEmail queue in DB
2010-04-19T02:33:21.560657-05:00fish2000https://djangosnippets.org/snippets/1993/<p>This is what I use to send simple status emails from my sites. Instead of a django.core.mail.send_mail call, which can take an irrritatingly, nondeterministically long time to return (regardless of error state), you can stow the emails in the database and rely on a separate interpreter process send them off …</p>
Freely redistributable