On 1/8/07, Gulopine <[EMAIL PROTECTED]> wrote:
I'm trying to extend the manager created for a ManyToMany relationship, and I can't seem to make it happen. There's loads of information available on extending basic managers, but I can't seem to find anything on the ManyToMany variety. Given that managers for ManyToMany fields are generated automatically from within a function, there's not really a class to override. I've tried a couple different ways of trying to override it, and I'm still working on another angle to try and get it to work, but I just thought maybe somebody already ran into this, or maybe there was some undocumented shortcut for overriding ManyToMany managers.
The ManyToMany managers are constructed using the default manager of the related class as a base; so, if you have: class Bar(Model): myBars = MyManager() class Foo(Model): bars = ManyToManyField(Bar) then foo.bars will use myBars as the base manager. However, this also means that Bar.objects is replaced by Bar.myBars. At present, there isn't a way to tell an M2M manager to use a different base manager than the class default manager. There has been some discussion in the past towards adding a mechanism for providing an override on the field definition, but these discussions haven't arrived at any concrete design or sample implementations.
For the record, the end result is that I need to limit the number of relationships a particular model can have to the other. To that end, I'm trying to override the add() method of the manager so that it throws an error if that limit has already been reached. So if there's a better way to achieve that goal than trying to override the ManyToMany manager, I'd be glad to hear it.
I can't think of an elegant solution, but you might be able to kludge a solution by defining a 'MyManager.my_add' function on the non-default manager for Bar that does your error checking, then defers to add(); not pretty, as you will need to use my_add() rather than add(), but it should work. The only other solution I can think of would be to validate at the form level, rather than the model level. Yours, Russ Magee %-) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---