Ticket #1280: models.py.diff
File models.py.diff, 2.3 KB (added by , 19 years ago) |
---|
-
django/contrib/contenttypes/models.py
1 1 from django.db import models 2 2 from django.utils.translation import gettext_lazy as _ 3 3 4 class Package(models.Model):5 label = models.CharField(_('label'), maxlength=20, primary_key=True)6 name = models.CharField(_('name'), maxlength=30, unique=True)7 class Meta:8 verbose_name = _('package')9 verbose_name_plural = _('packages')10 db_table = 'django_package'11 ordering = ('name',)12 13 def __repr__(self):14 return self.name15 16 4 class ContentType(models.Model): 17 5 name = models.CharField(_('name'), maxlength=100) 18 package = models.ForeignKey(Package, db_column='package') 19 python_module_name = models.CharField(_('python module name'), maxlength=50) 6 model = models.CharField(_('python model class name'), maxlength=100, unique=True) 20 7 class Meta: 21 8 verbose_name = _('content type') 22 9 verbose_name_plural = _('content types') 23 10 db_table = 'django_content_type' 24 ordering = ('package', 'name') 25 unique_together = (('package', 'python_module_name'),) 11 ordering = ('name',) 26 12 27 13 def __repr__(self): 28 return "%s | %s" % (self.package_id, self.name)14 return self.name 29 15 30 16 def get_model_module(self): 31 "Returns the Python model module for accessing this type of content." 32 return __import__('django.models.%s.%s' % (self.package_id, self.python_module_name), '', '', ['']) 17 "Returns the Python model class for accessing this type of content." 18 lastdot = self.model.rfind('.') 19 package = lastdot > 0 and self.model[:lastdot] or '' 20 module = __import__(package, '', '', ['']) 21 return getattr(module, self.model[lastdot+1:]) 33 22 34 23 def get_object_for_this_type(self, **kwargs): 35 24 """ … … 38 27 method. The ObjectNotExist exception, if thrown, will not be caught, 39 28 so code that calls this method should catch it. 40 29 """ 41 return self.get_model_module(). get_object(**kwargs)30 return self.get_model_module().objects.get_object(**kwargs)