[Solved]-Overriding Django's RelatedManager methods

4👍

I’m not sure what you need the override for – the default queryset already does what you want.

But to answer the question, you can define a custom Manager on the model and set use_for_related_fields=True to ensure it gets used as the automatic manager. See the documentation on controlling automatic Manager types.

0👍

I think I am having the same problem.

I have a custom manager that overrides self._db and get_query_set() to route it to different databases.

I dynamically created a model class, and has its _default_manager set with my custom manager.

This works for the class itself, but not for related field (foreign or many2many), even though I did set sets use_for_related_fields = True.

For related field, appending db_manager(dbname) (for example, record.managed_set.db_manager(dbname)) can fix all() method, but not for add() method.

To understand what I mean, see this django ticket: http://code.djangoproject.com/ticket/13358

I think it works for all(), but not add().

0👍

RelatedManager.add() calls RelatedManager._add_items() which calls Manager.bulk_create().

So if you extend Manager.bulk_create(), you might be able to achieve what you are after.

👤mehmet

Leave a comment