djangosnippets.org: Latest snippets tagged with 'inheritance'https://djangosnippets.org/tags/inheritance/2013-12-22T15:27:52.497281-06:00Inherit the standard url tag to include domain name
2013-12-22T15:27:52.497281-06:00atodorov-otbhttps://djangosnippets.org/snippets/3013/<p>This module extends the standard `url' template tag in Django and adds support for fully qualified domain name URLs. It also can be extended with simple URL load balancing techniques if desired.</p>
<p>See my blog for the background story:
<a href="http://atodorov.org/blog/2013/12/22/django-template-tag-inheritance-howto/">http://atodorov.org/blog/2013/12/22/django-template-tag-inheritance-howto/</a></p>
Freely redistributableGet derived model instance
2013-09-18T04:52:25.252590-05:00werehumanhttps://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 redistributableForm and ModelForm inheritance DRY
2013-06-03T03:49:18.380383-05:00evilclayhttps://djangosnippets.org/snippets/2925/<p>Modelform cant inhertit from forms. To solve this issue, split thing you wanto to inherit into filed definition and functionality definition. For modelform use the base_fields.update method as mentioned in the code.</p>
Freely redistributablePolymorphic inheritance ala SQLAlchemy
2010-10-03T10:01:25.101574-05:00gsakkishttps://djangosnippets.org/snippets/2216/<p>This is a different take on polymorphic inheritance, inspired by SQLAlchemy's approach to the problem.</p>
<p>The common Django approach (e.g. snippets 1031 & 1034, <a href="http://github.com/bconstantin/django_polymorphic">django_polymorphic</a>) is to use a foreign key to <code>ContentType</code> on the parent model and override <code>save()</code> to set the right content type automatically. That works …</p>
Freely redistributableChild aware model inheritance
2008-11-14T10:14:12.237993-06:00rixhttps://djangosnippets.org/snippets/1187/<p>Base models aren't aware of its inherited models, here is a quick solution to access child models from the base model.</p>
Freely redistributableextends_default
2008-11-05T21:36:29.967634-06:00daniellindsleyhttps://djangosnippets.org/snippets/1167/<p>Works exactly like the standard "extends" tag but enables one to fallback on a default template. This tag is LIMITED, as it falls back to the next template with the same name that DOES NOT contain "extends_default" (does NOT simulate full inheritance, just a single level).</p>
<p>A partial solution to …</p>
Freely redistributableParentModel and ChildManager for Model Inheritance
2008-09-08T01:32:13.135509-05:00jpwattshttps://djangosnippets.org/snippets/1037/<p>This is the approach I've taken to access instances of child models from their parent. Functionally it's very similar to snippets <a href="http://www.djangosnippets.org/snippets/1031/">1031</a> and <a href="http://www.djangosnippets.org/snippets/1034/">1034</a>, but without the use of <code>django.contrib.contenttypes</code>.</p>
<p>Usage:</p>
<pre><code>class Post(ParentModel):
title = models.CharField(max_length=50)
objects = models.Manager()
children = ChildManager()
def __unicode__(self):
return self.title
def get_parent_model(self): …</code></pre>
Freely redistributableModel inheritance with content type and inheritance-aware manager
2008-09-07T19:33:33.719265-05:00dan90https://djangosnippets.org/snippets/1034/<p>inspired by crucialfelix's <a href="http://www.djangosnippets.org/snippets/1031/">inheritance hack</a>, which was a far better method of fetching a model's subclassed version from an instance than my own, i decided to bake his snippet in to my own inheritance hack, which i think benefits both. the result is a query set that returns subclassed …</p>
Freely redistributableModel inheritance with content type
2008-09-07T07:47:26.006317-05:00crucialfelixhttps://djangosnippets.org/snippets/1031/<p>Contact is a parent class. Subclasses might be Company, Person, Artist, Label etc.
Basic address, email etc. fields can be added to the parent class and all subclasses will have those.</p>
<p>Having searched your database for contacts (undifferentiated by class) you then want to reload the chosen object as the …</p>
Freely redistributableCustom Django manager that excludes subclasses
2008-08-07T19:52:54.136939-05:00sciyoshihttps://djangosnippets.org/snippets/950/<p>When you're using Django model inheritance, sometimes you want to be able to get objects of the base class that aren't instances of any of the subclasses. You might expect the obvious way of doing this, <code>SuperModel.objects.filter(submodel__isnull=True)</code>, to work, but unfortunately it doesn't. (Neither does <code>SuperModel.objects.filter(submodel__supermodel_ptr=None)</code>, or any …</p>
Freely redistributableMultiple inheritance of newforms and modelforms
2008-04-12T06:18:53.493534-05:00simonhttps://djangosnippets.org/snippets/703/<p>If you try to use multiple inheritance with a modelform (to mix in some fields from an already existing form class for example) you'll get the following rather terrifying error:</p>
<blockquote>
<p>"Error when calling the metaclass bases metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of …</p></blockquote>
Freely redistributableDjango Model Inheritance
2008-01-01T00:10:01.389194-06:00sridhttps://djangosnippets.org/snippets/529/<p>This snippet shows an alternative and interesting way to do Model Inheritance in Django. For description of the code, follow the inline comments.
</p>
Freely redistributableDRY with common model fields
2007-07-14T13:20:54.792576-05:00miracle2khttps://djangosnippets.org/snippets/317/<p>If you have many models that all share the same fields, this might be an option. Please note that order matters: Your model need to inherit from TimestampedModelBase first, and models.Model second. The fields are added directly to each model, e.g. while they will be duplicated on the database level, …</p>
Freely redistributable