Question

I have two models, say, Question and Topic.

I am trying to add methods to the Question model's custom manager, e.g. some method that filters by Topic.

I can't seem to use the other manager's code for this (cannot import Topic either, so I can't do Topic.objects...)

In class QuestionManager

def my_feed(self, user):
       topics = TopicManager().filter(user=user) # 1st approach
       #topics = Topic.objects.filter(user=user) # 2nd line
       # do something with topics

class TopicManager ....

Using 1st approach, I get the following error:

virtualenv/local/lib/python2.7/site-packages/django/db/models/sql/query.pyc in get_meta(self)
    219         by subclasses.
    220         """
--> 221         return self.model._meta
    222 
    223     def clone(self, klass=None, memo=None, **kwargs):

AttributeError: 'NoneType' object has no attribute '_meta'

I can't use the 2nd line since I can't import Topic, since Topic depends on the TopicManager in this file. Is there a workaround for this?

Was it helpful?

Solution

You can't use a manager directly, in any circumstance. You always access it via the model class.

If you can't import the model at the top of the file because of a circular dependency, you can simply import it inside the method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top