Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use custom managers in reverse relations?

I want to get the related object references and I want to use a custom manager.

I see there has been already a question about it, but its not open anymore. So im creating a new one Edit: And its outdated.

This is pretty much what im trying to do: Related objects reference with custom manager

b.entry_set.all() # This will use the default Manager
b.custom_manager.entry_set.all() # This should use my custom model manager

I see the ticket is closed but i havent found a solution to it yet. https://code.djangoproject.com/ticket/3871

Im using django 1.6.5

like image 477
Agey Avatar asked Dec 11 '14 16:12

Agey


People also ask

How do I use custom Manager in Django?

If you want to make a manager default and that manager object is not defined first then you can define it as default manager by setting default_manager_name of Meta class equal to that object name. You can do anything you want with custom managers.

Why might you want to write a custom model Manager?

Custom managers. You can use a custom Manager in a particular model by extending the base Manager class and instantiating your custom Manager in your model. There are two reasons you might want to customize a Manager : to add extra Manager methods, and/or to modify the initial QuerySet the Manager returns.

Where are model managers used?

Rahul Mishra. Django Model Manager is the interface through which database query operations are provided to Django models. objects is the default model manager and each model needs to have at least one model manager. Let's see a few use-cases when we can use model managers and when to use custom model managers.

What does Models Manager class do?

The model managers is a major tool of Django's ORM that developers use to interact with the database. Fundamentally a manager is a Python class that inherits from the Manager class defined in Django's db. models package. According to Django Docs, at least one Manager exists for every model in a Django application.


1 Answers

I have found only this solution:

instance.entry_set(manager='custom_manager')
like image 85
Sławek Kabik Avatar answered Oct 09 '22 11:10

Sławek Kabik