djangosnippets.org: Latest snippets tagged with 'models'http://djangosnippets.org/tags/models/2019-08-17T01:45:31.434454-05:00Model mixin to return all field data in a dictionary
2019-08-17T01:45:31.434454-05:00rixxhttp://djangosnippets.org/snippets/10678/<p>Get all field data, e.g. for an export.</p>
Freely redistributableCompare objects list and get a list of object to inserted or updated
2015-07-09T15:36:20.454762-05:00paridinhttp://djangosnippets.org/snippets/10509/<p><strong>Problem</strong>
You have an input <code>json</code> with which you will create a list of objects, you have to validate that the object will be created if it not exists, if exists determine whether to upgrade or discard depending of they have not undergone any changes.</p>
<p>Solution
1) With the input …</p>
Freely redistributableRestMiddleware - automatically load objects from url-path
2014-08-27T04:58:07.604108-05:00dhkehttp://djangosnippets.org/snippets/3090/<p>Automatically load models when their IDs appear in the URL-path.</p>
<p>See docstrings for usage and example.</p>
Freely redistributableAnother Multiform
2014-08-27T04:51:45.862662-05:00dhkehttp://djangosnippets.org/snippets/3089/<p>MultiForm and MultiModelForm</p>
<p>Based on a PrefixDict class I wrote and thus very lean. Lacks a little documentation, though</p>
<pre><code>class MyMultiForm(ModelMultiForm):
class Meta:
localized_fields = '__all__'
form_classes = OrderedDict((
('form1', Form1),
('form2', Form2),
))
</code></pre>
<p>Subfields are named <code>form-name</code> <code>prefix_sep</code> <code>subfield-name</code>. <code>prefix_sep</code> defaults to <code>-</code>. For access in templates, …</p>
Freely redistributableIntegerRangeField
2014-01-01T03:50:08.638839-06:00moemenhttp://djangosnippets.org/snippets/3016/<p>Django models IntegerField that get max_value and min_value in it's constructor and validate on it.</p>
<p>It's initialize the formfield with min_value and max_value, too.</p>
Freely redistributableDjango admin autoregister
2013-11-08T08:12:44.251012-06:00Miminohttp://djangosnippets.org/snippets/3002/<p>Official GitHub page: https://github.com/Mimino666/django-admin-autoregister</p>
<p>One call to autoregister_admin() automatically creates and registers admin views for all the models in the specified module with intelligent linking between ForeignKey, OneToOneField and ManyToManyField fields.</p>
Freely redistributableGet derived model instance
2013-09-18T04:52:25.252590-05:00werehumanhttp://djangosnippets.org/snippets/2992/<p>Get derived model without storing their names or content types in databases. You write only one line, it expands into only one SQL-query (with many LEFT OUTER JOIN's).</p>
<p>Model definition example:</p>
<pre><code>class BaseModel(models.Model):
foo = models.IntegerField(null=True)
derived = DerivedManager()
class FirstChild(BaseModel):
bar = models.IntegerField(null=True)
class SecondChild(BaseModel):
baz = models.IntegerField(null=True)
</code></pre>
<p>How …</p>
Freely redistributableExtended i18n base model
2013-04-08T23:32:21.041419-05:00alcinnzhttp://djangosnippets.org/snippets/2907/<p>This snippet is an extension of <a href="http://djangosnippets.org/snippets/855/">i18n base model for translatable content</a> so all the same usage applies.</p>
<p>I have extended this module in several ways to make it more fully featured.</p>
<ul>
<li>
<p><code>I18NMixin</code> can be an additional (via multiple inheritance) or alternative superclass for your models juxtaposed with an <code>I18NModel …</code></p></li></ul>
Freely redistributabledecorator to add GUID Field to Django Models
2013-02-03T16:02:44.276473-06:00trubliphonehttp://djangosnippets.org/snippets/2888/<p>A decorator to add a GUID Field to a Django Model. There are other bits of code out there that do similar things, but it was important for the field to have a unique value <em>before</em> it is saved in the database. The contribute_to_class method therefore registers the field class …</p>
Freely redistributablePatchModelForm - A ModelForm subclass with the semantics of the PATCH HTTP method
2012-05-26T22:48:23.189925-05:00gnrfanhttp://djangosnippets.org/snippets/2758/<p>Use this class to partially update one or more fields of a model. Only the fields that are bound to the form via the "data" parameter in the constructor get updated. All automatically generated fields have their "required" attribute set to False.</p>
<p>Example 1:</p>
<pre><code>from django.contrib.auth.models import User
class PatchUserForm(PatchModelForm): …</code></pre>
Freely redistributableBinding signals to abstract models
2012-05-15T11:25:45.770359-05:00andreterrahttp://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 an abstract …</p>
Freely redistributable Aggiornare i Content Types e i Permessi del Model di una Tabella nell Admin
2012-05-15T04:06:45.026804-05:00dario.agliottonehttp://djangosnippets.org/snippets/2746/<p>Script per Aggiornare i ContentTypes e i Permessi del Model di una Tabella nell Admin
<a href="http://www.darioagliottone.it/blog">Django Blog</a></p>
Freely redistributableRemoving old ImageFields and FileFields when updating through admin
2012-05-04T06:03:10.000027-05:00alejandro.alonsohttp://djangosnippets.org/snippets/2742/<p>Example:</p>
<p>admin.site.register(YourCoolModel, CustomModelAdmin)</p>
Freely redistributablePrefetch id for Postgresql backend
2012-04-17T00:54:16.325766-05:00bretthhttp://djangosnippets.org/snippets/2731/<p>When uploading a file or image, you need to put it somewhere that's not going to be orphaned by a change in the model. You could use a globally unique value like a uuid, but the django id autofield is a much shorter surrogate field that can be used in …</p>
Freely redistributableUnit testing Django model mixins
2012-01-26T13:43:07.672809-06:00michaelmiorhttp://djangosnippets.org/snippets/2663/<p>This facilitates unit tests for model mixins in Django. For more info, see <a href="https://michael.mior.ca/blog/unit-testing-django-model-mixins/">my blog</a>.</p>
Freely redistributable