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: donato.gr@… 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

Description

Is is not possible to specify a database connection when using

ContentType.objects.get_for_model(model)

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


If I try:

ContentType.objects.using(other_database).get_for_model(model)

I get the following error:

 'QuerySet' object has no attribute 'get_for_model'

Change History (6)

comment:1 Changed 6 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 3 years ago by anonymous

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

comment:3 Changed 3 years ago by anonymous

Resolution: invalid
Status: closednew

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

auth
session
ContentType

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 options.py 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 admin.py 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: invalid
Status: newclosed

Please read https://docs.djangoproject.com/en/dev/topics/db/multi-db/#using-managers-with-multiple-databases, 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