djangosnippets.org: Latest snippets tagged with 'model'https://djangosnippets.org/tags/model/2017-01-14T05:46:03-06:00CNPJ and CPF Validation for Models2017-01-14T05:46:03-06:00tcarranzahttps://djangosnippets.org/snippets/10601/<p>The code was placed inside a helper file without using a class. The Django validator was not designed to work with validator classes, it would appear, so retrieving the value from the field proved to be a hassle. Just create a helper file, import it on your model, and use ...</p>
Freely redistributableModify properties on fields inherited from an abstract superclass2014-06-29T07:08:37-05:00andybakhttps://djangosnippets.org/snippets/3079/<p>See http://stackoverflow.com/questions/927729/how-to-override-the-verbose-name-of-a-superclass-model-field-in-django/24475838#24475838</p>
Freely redistributableautogenerated UUID model field 2014-03-06T12:02:47-06:00kedarhttps://djangosnippets.org/snippets/3034/<p>Provides UUIDField for your models. This version creates very short UUID represenation (21 chars) when the record is added eg. in admin. Generated ids are safe to be used in URLs.</p>
<p>You can put represent it in admin as </p>
<p>'readonly_fields=("uuid",)'</p>
Freely redistributableModel Mixin to Save Only Changed Fields2013-08-25T23:18:29-05:00karanlyonshttps://djangosnippets.org/snippets/2985/<p><strong><a href="https://github.com/karanlyons/django-save-the-change">Improved and Released as Save The Change.</a></strong></p>
<p>Django 1.5 added the <code>update_fields</code> <code>kwarg</code> to <code>Model.save()</code>, which allows the developer to specify that only certain fields should actually be committed to the database. However, Django provides no way to automatically commit only changed fields if they're not specified ...</p>
Freely redistributableA Lazy ModelChoiceField implementation2013-07-16T12:13:39-05:00alecdoticohttps://djangosnippets.org/snippets/2973/<p>Sometimes we may need to generate a <em>ModelChoiceField</em> in which choices are generated at runtime, depending on the locale language. The snippet generates a <em>ChoiceField</em> based on a queryset and a specific attribute of the Model, ordering the choices by the attribute content in the locale language.</p>
<p><strong>Usage example</strong> (inside ...</p>
Freely redistributableGravatar support in model save override2013-02-18T05:57:44-06:00JoeLinuxhttps://djangosnippets.org/snippets/2894/<p>Overridden save() method that adds Gravatar support for a user with a profile photo field (and presumably an email field). Checks to see if user has provided a photo. If not, then query Gravatar for a possible photo. Finally, if Gravatar does not have an appropriate photo for this user ...</p>
Freely redistributableCompressedTextField for Django 1.4+2013-02-15T10:32:25-06:00devhuluhttps://djangosnippets.org/snippets/2893/<p>This snippet <em>updates</em> http://www.djangosnippets.org/snippets/383/ and http://www.djangosnippets.org/snippets/1495/ for Django 1.4+, and adds support for sqlite3 and south. Original snippet text: A CompressedTextField to transparently save data gzipped in the database and uncompress at retrieval.</p>
Freely redistributablecreate_model_instances management command2013-01-21T10:50:40-06:00Lacrymologyhttps://djangosnippets.org/snippets/2881/<p>This management command is run like this: <code>./manage.py -a someapp filename.cfg</code></p>
<p>it looks in <code>someapp</code>'s directory for a file called <code>/config/filename.cfg</code> with the format explained in the help text, and creates the model instances described in the config file.</p>
<p>It uses the configobj module.</p>
<p>this ...</p>
Freely redistributableMultiSelectField with comma separated values (Field + FormField)2012-05-17T17:23:26-05:00quinodehttps://djangosnippets.org/snippets/2753/<p>Daniel Roseman's snippet, updated will all fixes mentioned in the comments of the first version + some other things to make it work under Django 1.4.<br />
South, and dumpdata are working.</p>
<p>There's an ugly int(....) at the validate function in order to cast each value as an integer ...</p>
Freely redistributableBinding signals to abstract models2012-05-15T11:25:45-05:00andreterrahttps://djangosnippets.org/snippets/2749/<h2>Intro</h2>
<p>I found a question on SO for which Justin Lilly's answer was correct but not as thorough as I'd like, so I ended up working on a simple snippet that shows how to bind signals at runtime, which is nifty when you want to bind signals to ...</p>
Freely redistributableTrue Unique Boolean Model Decorator2012-05-15T06:48:31-05:00kunitokihttps://djangosnippets.org/snippets/2747/<p>This class decorator will help you when you want to keep a unique boolean (think a 'default' field which should only be only one set to true in a group).</p>
<p>The interesting thing with this, is that it's possible to assign a subset of fields to be used, so ...</p>
Freely redistributableEasier chainability with custom QuerySets2012-03-11T15:00:34-05:00bendavis78https://djangosnippets.org/snippets/2713/<p>Django allows you to specify your own ModelManager with custom methods. However, these methods are chainable. That is, if you have a method on your PersonManager caled men(), you can't do this:</p>
<pre><code>Person.objects.filter(birth_date__year=1978).men()
</code></pre>
<p>Normally, this isn't a problem, however your app may be ...</p>
Freely redistributableTrue Unique Boolean Decorator2012-02-06T09:23:59-06:00kunitokihttps://djangosnippets.org/snippets/2676/<p>Useful when you want to keep only one instance of a model to be the default one.</p>
<p>This is a decorative way of doing the same as in http://djangosnippets.org/snippets/1830/</p>
<p>Can this be made better as a class decorator (not having to declare explicitly the save method) ?</p>
Freely redistributableDump a model instance and related objects as a Python data structure2012-01-31T02:21:00-06:00akaiholahttps://djangosnippets.org/snippets/2670/<p>This utility makes a text dump of a model instance, including objects related by a forward or reverse foreign key. The result is a hierarchical data structure where</p>
<ul>
<li>
<p>each instance is represented as a list of fields,</p>
</li>
<li>
<p>each field as a (&lt;name&gt;, &lt;value&gt;) tuple,</p>
</li>
<li>
<p>each &lt;value&gt; as a primitive type ...</p></li></ul>
Freely redistributableValidating Model subclass2012-01-24T18:13:31-06:00slacyhttps://djangosnippets.org/snippets/2662/<p>If you inherit from ValidatedModel instead of from models.Model, then full_clean() will be called before save().<br />
</p>
<p>So, add validators to your field definitions, and all your fields will be validated before they go to the database. </p>
<p>The same thing can be accomplished with a pre_save signal, but the code ...</p>
Freely redistributable