2010-04-06 2 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이는 다음과 같습니다이 '콘텐츠 _이'

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) 

내가 알고 싶은 다음 '콘텐츠 _'가 사용되는지?

+4

힌트 : Django의 설명서를 읽으십시오. 그것은 정말로 좋으며 대부분의 질문을 다루고 있습니다. –

+1

뭐가 잘못 되었니? 때때로 설명서를 읽으십시오! –

+0

당신에게 문제가 있습니까? 아들 아? – zjm1126

답변

1

ContentType은 많은 다른 모델에 외래 키가 있고 하나의 쿼리에서이 모든 것을 얻을 수있는 모델을 사용하려는 경우에 사용됩니다.

예 : City 모델과 Restaurant 모델 및 Pub 모델도 있습니다. 당신이 문서에서 확인할 수

2 개 쿼리가 필요합니다 도시의 모든 레스토랑과 술집을 얻으려면,

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

일반 외래 키를 사용하여, 당신은 그것을 단일 쿼리를 만들 수 있습니다 http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#ref-contrib-contenttypes