Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 843
JTN Avatar asked Oct 11 '25 17:10

JTN


1 Answers

In order to get the ContentType of a proxy model, you need to pass in the argument for_concrete_model=False into get_for_model().

Example:

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

See the official docs for more information.