Generic Model

 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

  1. Model merging function by xaralis 3 years, 4 months ago
  2. Manager method for limiting GenericForeignKey queries by zerok 5 years, 8 months ago
  3. URL models by diverman 4 years, 6 months ago
  4. unique_together with many to many fields by j4nu5 2 years, 7 months ago
  5. Generate Model Data. Lots of Options. by bl4th3rsk1t3 4 years ago

Comments

ubernostrum (on March 1, 2007):

Also, note that the GenericForeignKey field included in Django allows a fairly clean implementation of this.

#

(Forgotten your password?)