2012-05-11 51 views
2

我如何覆盖关系管理器?例如:覆盖django的RelatedManager for model

user.entry_set # django.db.models.fields.related.RelatedManager 

但我需要我自己的经理。我试试这个代码,但是这不起作用:

class EntryManager(models.Manager): 
    use_for_related_fields = True 

谢谢。

+1

为了好奇,为什么你认为你需要一个自定义管理器? –

+0

为什么'use_for_related_fields'“工作”?这就是*用于*的地方,所以如果它不起作用,那么代码中就有些东西搞砸了。 *总是发布代码。* –

+0

@ChrisPratt实际上,它只适用于'OneToOneField'关系([ticket](https://code.djangoproject.com/ticket/14891))。 – DrTyrsa

回答

-1

你的任务需要相当难以维护和理解的黑客代码(如果甚至可能的话)。

这是怎么回事?

class EntryManager(models.Manager): 
    def by_user(self, user): 
     # do anything you want 

# usage 
Entry.objects.by_user(user=user)