Ticket #1280: models.py.diff

File models.py.diff, 2.3 KB (added by Esaj, 16 years ago)
  • django/contrib/contenttypes/models.py

     
    11from django.db import models
    22from django.utils.translation import gettext_lazy as _
    33
    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.name
    15 
    164class ContentType(models.Model):
    175    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)
    207    class Meta:
    218        verbose_name = _('content type')
    229        verbose_name_plural = _('content types')
    2310        db_table = 'django_content_type'
    24         ordering = ('package', 'name')
    25         unique_together = (('package', 'python_module_name'),)
     11        ordering = ('name',)
    2612
    2713    def __repr__(self):
    28         return "%s | %s" % (self.package_id, self.name)
     14        return self.name
    2915
    3016    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:])
    3322
    3423    def get_object_for_this_type(self, **kwargs):
    3524        """
     
    3827        method. The ObjectNotExist exception, if thrown, will not be caught,
    3928        so code that calls this method should catch it.
    4029        """
    41         return self.get_model_module().get_object(**kwargs)
     30        return self.get_model_module().objects.get_object(**kwargs)
Back to Top