Opened 6 years ago

Closed 3 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 6 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

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 3 years ago by anonymous

  • Easy pickings unset
  • Severity set to Normal
  • Type set to Bug
  • UI/UX unset
  • Version changed from 1.2 to 1.3

comment:3 Changed 3 years ago by anonymous

  • Resolution invalid deleted
  • Status changed from closed to new

comment:4 Changed 3 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 3 years ago by Abhilash Joseph C <abhi.jo89@…>

  • Cc abhi.jo89@… added

comment:6 Changed 3 years ago by anonymous

  • Resolution set to invalid
  • Status changed from new to closed

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