How to get the app a Django model is from?

Posted by e-satis on Stack Overflow See other posts from Stack Overflow or by e-satis
Published on 2010-04-30T06:29:24Z Indexed on 2010/04/30 6:37 UTC
Read the original article Hit count: 170

Filed under:
|
|

I have a model with a generic relation:

TrackedItem --- genericrelation ---> any model

I would like to be able to generically get, from the initial model, the tracked item.

I should be able to do it on any model without modifying it.

To do that I need to get the content type and the object id. Getting the object id is easy since I have the model instance, but getting the content type is not: ContentType.object.filter requires the model (which is just content_object.__class__.__name__) and the app_label.

I have no idea of how to get in a reliable way the app in which a model is.

For now I do app = content_object.__module__.split(".")[0], but it doesn't work with django contrib apps.

© Stack Overflow or respective owner

Related posts about django

Related posts about model