Opened 7 years ago

Closed 5 years ago

#14945 closed Bug (invalid)

ContentType.objects.get_for_model doesn't allow using multiple databases

Reported by:… Owned by: nobody
Component: Database layer (models, ORM) Version: 1.3
Severity: Normal Keywords: ContentType, multi-database, get_for_model
Cc: abhi.jo89@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Is is not possible to specify a database connection when using


This should be, because the same model can have a different in each database.

If I try:


I get the following error:

 'QuerySet' object has no attribute 'get_for_model'

Change History (6)

comment:1 Changed 7 years ago by Russell Keith-Magee

Resolution: invalid
Status: newclosed

get_for_model() is a manager method, so you need to use db_manager(), not using(). This gives you an instance of the manager that has been forced to a particular database, rather than a queryset that has been directed to a particular database.

comment:2 Changed 5 years ago by anonymous

Easy pickings: unset
Severity: Normal
Type: Bug
UI/UX: unset
Version: 1.21.3

comment:3 Changed 5 years ago by anonymous

Resolution: invalid
Status: closednew

comment:4 Changed 5 years ago by Abhilash Joseph C

Sir i need to override the function "render_change_form" and pass the "content_type_id" , because my django_content_type table is saved in other database .

My auth_db data base has the following applications


and all my project related models use default db

When i override the "change_view" and pass two object ids to "extra_context" i get an error "(1146, "Table 'movie.django_content_type' doesn't exist")

This is because in tries to load the "content_type_id" from the default db but"content_type_id" won't be available.So i tried to override "render_change_form" function in and passing "content_type_id = ContentType.objects.using("auth_db").get_for_model(self.model).id" .It gives me an error "'QuerySet' object has no attribute 'get_for_model'"
Therefore It would be apreciable if you would permit developers the accession to content_type_id though "ContentType.objects.using("auth_db").get_for_model(self.model).id"

Thank you

comment:5 Changed 5 years ago by Abhilash Joseph C <abhi.jo89@…>

Cc: abhi.jo89@… added

comment:6 Changed 5 years ago by anonymous

Resolution: invalid
Status: newclosed

Please read, the documentation for the db_manager method referenced by Russell above. What you are asking to do is possible, just not using the exact calls you are trying. Please try the documented way to achieve what you are trying to do.

Note: See TracTickets for help on using tickets.
Back to Top