2010-04-06 164 views
-1
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,)) 

和ContentType的是:這是什麼 'CONTENT_TYPE' 意味着

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) 

我想知道:什麼是 'CONTENT_TYPE' 使用?

+4

正如一個提示:閱讀Django的文檔。它非常好,涵蓋了大部分問題。 – 2010-04-06 13:04:49

+1

你有什麼問題?請偶爾閱讀文檔! – 2010-04-06 13:38:12

+0

你有什麼問題?某某男孩? – zjm1126 2010-04-07 01:51:35

回答

1

ContentType用於其中,例如,您希望使用許多不同型號具有外鍵並且能夠在單個查詢中獲得所有外鍵的模型。

例如:您有城市模型,也有餐廳模型和酒吧模型。

爲了獲得在這個城市的所有餐館和酒吧將需要2個查詢,

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

通過使用通用外鍵,你可以把它一個單一的查詢,你可以從文檔檢查:http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#ref-contrib-contenttypes