tags:

views:

64

answers:

2
content_type = ContentType.objects.get_for_model(Map)

    maps = maps.extra(select=SortedDict([
        ('member_count', MEMBER_COUNT_SQL),
        ('topic_count', TOPIC_COUNT_SQL),
    ]), select_params=(content_type.id,))

and the ContentType is:

class ContentType(models.Model):
    name = models.CharField(max_length=100)
    app_label = models.CharField(max_length=100)
    model = models.CharField(_('python model class name'), max_length=100)
    objects = ContentTypeManager()

    class Meta:
        verbose_name = _('content type')
        verbose_name_plural = _('content types')
        db_table = 'django_content_type'
        ordering = ('name',)
        unique_together = (('app_label', 'model'),)

    def __unicode__(self):
        return self.name

    def model_class(self):
        "Returns the Python model class for this type of content."
        from django.db import models
        return models.get_model(self.app_label, self.model)

    def get_object_for_this_type(self, **kwargs):
        """
        Returns an object of this type for the keyword arguments given.
        Basically, this is a proxy around this object_type's get_object() model
        method. The ObjectNotExist exception, if thrown, will not be caught,
        so code that calls this method should catch it.
        """
        return self.model_class()._default_manager.using(self._state.db).get(**kwargs)

    def natural_key(self):
        return (self.app_label, self.model)

i want to know: what is the 'content_type' used for ??

+2  A: 

It's used for generic relations, among other things.

Ignacio Vazquez-Abrams
A: 

ContentType is used where, say you want to use a model that many different models have a foreign key to and be able to obtain them all in a single query.

Eg: You have a City model, also Restaurant model and a Pub model.

To obtain all the restaurants and pubs in the city will need 2 queries,

city.restaurant_set.all()
city.pub_set.all()

By using Generic foreign keys, you can make it a single query, as you can check from the documentation: http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#ref-contrib-contenttypes

Lakshman Prasad