Search code examples
pythondjangopython-2.7django-modelsdjango-1.7

ContentType.objects.get_for_model(obj) returning base class model when used on a proxy model object


I have a proxy model derived from another model. Now I create object of this proxy model and try to find out the content type object using ContentType.objects.get_for_model(obj) it returns the base class content type object rather than giving me the proxy model content type. Im using django 1.7.8.

class BaseModel(models.Model):
    field1 = models.CharField(max_length=200)
    field1 = models.CharField(max_length=200)


class ProxyModel(BaseModel):
    class Meta:
        proxy = True

now i am getting an object of proxy model

proxy_obj = ProxyModel.objects.get(field1=1)

and trying to find the content type class of the proxy_obj

content_type = ContentType.objects.get_for_model(proxy_obj)

But this yields me the content type object of BaseModel instead of ProxyModel. Why is this behaving in a absurd way? Or am i doing something wrong?


Solution

  • From django-docs for get_for_model method:

    Takes either a model class or an instance of a model, and returns the ContentType instance representing that model. for_concrete_model=False allows fetching the ContentType of a proxy model.

    You have to pass for_concrete_model=False with get_for_model(), like this:

    content_type = ContentType.objects.get_for_model(proxy_obj, for_concrete_model=False)