1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | from django.db import models
class GenericModel(models.Model):
app_name = models.CharField(maxlength=64)
model_name = models.CharField(maxlength=64)
object_id = models.IntegerField()
def get_model_class(self):
return models.get_model(self.app_name, self.model_name)
def get_this_object(self):
return self.get_model_class()._default_manager.get(**kwargs)
def __str__(self):
return self.get_this_object().__str__()
|
More like this
- Manager method for limiting GenericForeignKey queries by zerok 4 years, 10 months ago
- URL models by diverman 3 years, 8 months ago
- Create permissions for proxy models by charettes 1 year, 4 months ago
- Command to dump data as a python script by willhardy 4 years, 12 months ago
- Update ContentTypes and Permissions without syncdb by paltman 5 years, 2 months ago
Comments
Also, note that the
GenericForeignKeyfield included in Django allows a fairly clean implementation of this.#