djangosnippets.org: Latest snippets tagged with 'mysql'https://djangosnippets.org/tags/mysql/2013-08-12T21:30:02.634074-05:00Many 2 Many Admin Ordering with Mysql
2013-08-12T21:30:02.634074-05:00visik7https://djangosnippets.org/snippets/2979/<p>My Models has a FK to translations and also a many 2 many to categories which also them are translated</p>
<p>With this code I concatenate the translation of the categories and allow the changelist to order them.</p>
<p>works only on mysql but you can adapt to your DB</p>
<p>SET SESSION …</p>
Freely redistributableModel Locking Mixin & Decorator (MySQL Advisory Locks)
2011-05-20T15:39:27.902324-05:00piohttps://djangosnippets.org/snippets/2443/<p>This code provides a mixin and decorator which, when used together, can provide advisory locking on model methods. It provides locking by using MySQL's advisory lock system. See the example at the bottom of the code.<br />
</p>
<p>This is a convenient and easy way to guarantee your model methods have exclusive …</p>
Freely redistributableArbitrary auto-generated primary keys
2010-07-23T11:23:53.045296-05:00gsakkishttps://djangosnippets.org/snippets/2122/<p>Auto-incremented primary keys are the default in Django and they are supported natively in most databases but for anything more complex things are less trivial. DB sequences are not standard, may not be available and even if they are, they are typically limited to simple integer sequence generators. This snippet …</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 redistributableDrop all tables in MySQL database
2010-01-28T08:34:28.736365-06:00mpasternackihttps://djangosnippets.org/snippets/1896/<p>Drops all tables in MySQL table (either managed by Django or not). Useful when resetting the database with no CREATE DATABASE privileges. MySQL-only.</p>
Freely redistributableModels with database views
2009-06-13T15:39:35.926143-05:00divermanhttps://djangosnippets.org/snippets/1567/<p>This example shows, how to use database views with django models. NewestArticle models contains 100 newest Articles. Remember, that NewestArticle model is read-only. Tested with mysql.</p>
Freely redistributableComposite Indexing for MySQL
2009-05-27T23:23:30.477047-05:00fongandrewhttps://djangosnippets.org/snippets/1534/<p>A quick and dirty hack for composite indexing if you need it. Drop this into a models.py or some other place where it'll be loaded along with the rest of Django on start up.</p>
<p>Then add an _index_together tuple specifying the fields you want a composite index on.</p>
Freely redistributableOld MySQL Password Hash
2009-05-15T10:38:28.376493-05:00tbackhttps://djangosnippets.org/snippets/1508/<p>A python implementation of the old MySQL PASSWORD() function.</p>
<p>This is insecure. There is a reason MySQL changed this in version 4.1. </p>
<p>Use it only if you have to!</p>
Freely redistributabledumpdata/loaddata with MySQL and ForeignKeys, as django command
2009-04-21T17:37:34.933099-05:00brondsemhttps://djangosnippets.org/snippets/1457/<p>Based on <a href="http://www.djangosnippets.org/snippets/662/">http://www.djangosnippets.org/snippets/662/</a> and updated to be runnable as custom django management command. Also added option support for --exclude=someapp --exclude=otherapp.SomeModel</p>
<p>From original description:
InnoDB tables within MySQL have no ability to defer reference checking until after a transaction is complete. This prevents most dumpdata/loaddata cycles unless the dump order falls …</p>
Freely redistributableget next mysql autoincrement value
2009-04-04T05:36:12.713954-05:00bernie2004https://djangosnippets.org/snippets/1415/<p>i use this to get the pk of a record before creation,
in my scenario to name an uploaded image:
</p>
<pre><code>def UPLOADTO( i,n ):
if not i.id:
id = get_nextautoincrement( i.__class__ )
else:
id = i.id
return str(id)+'.jpg'
</code></pre>
Freely redistributableClean up expired django.contrib.session's in a huge MySQL InnoDB table
2008-12-31T16:09:44.045570-06:00patsplathttps://djangosnippets.org/snippets/1273/<p>While django provides the <code>django_admin.py cleanup</code> script, if sessions get out of control sometimes you have to go lower level to get everything cleaned up. If the problem gets out of hand and you hit the resource limits of the machine, it is very difficult to get anything done in …</p>
Freely redistributableEncryptField
2008-09-19T13:50:50.761889-05:00volksmanhttps://djangosnippets.org/snippets/1067/<p>I was trying to create a custom field to use the mysql encrypt() function on some data I wanted to store in the DB. initcrash on IRC pointed me to <a href="https://tracpub.yaco.es/cmsutils/browser/trunk/db/fields.py?rev=66">this code</a> which I butchered as best as my little brain could. Amazingly enough I got it working (thanks to …</p>
Freely redistributablelock tables decorator
2008-09-04T06:48:55.187884-05:00seandonghttps://djangosnippets.org/snippets/1018/<p>sometimes,you need to lock one or more table to ensure the integrity of data.may be it's help to you.</p>
Freely redistributableLocking tables
2008-06-30T03:49:32.722453-05:00miohtamahttps://djangosnippets.org/snippets/833/<p>Sometimes you need to prevent concurrent access to update/calculate some properties right. Here is (MySQL) specific example to lock one table with new object manager functions.</p>
Freely redistributable