1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
class Place(models.Model):
name = models.CharField('name')
content_type = models.ForeignKey(ContentType)
# Child model
child = generic.GenericForeignKey(fk_field='id')
def save(self, **kwargs):
if not self.pk:
self.content_type = ContentType.objects.get_for_model(self)
super(Place, self).save(**kwargs)
class Restaurant(Place):
pass
|
More like this
- Custom Django manager that excludes subclasses by sciyoshi 4 years, 10 months ago
- ContentType Form by nosrednakram 3 years, 9 months ago
- Model inheritance with content type and inheritance-aware manager by dan90 4 years, 9 months ago
- Get child model by Hangya 5 years, 1 month ago
- Polymorphic inheritance ala SQLAlchemy by gsakkis 2 years, 8 months ago
Comments
May be?
#
The
get_for_modelmethod accepts an instance also, I think it'll work in both cases.#
This code is brittle, as I explain here. It's easy to make content_type have the wrong value, simply by loading and saving a Place object (that is actually a Restaurant too).
The fix is to only assign to self.content_type once, when the object is initially created (i.e. wrap the assignment in "if not self.id").
#
You're right carljm, the code can be improved with your fix.
In my use, a Place is never created by itself, only through child classes, so I'd never got affected.
I'll fix the code, thanks for the tip.
#