Manager isn't accessible via model instances

前端 未结 6 1422
余生分开走
余生分开走 2020-12-13 05:19

I\'m trying to get model objects instance in another one and I raise this error :

 Manager isn\'t accessible via topic instance

Here\'s my

6条回答
  •  借酒劲吻你
    2020-12-13 06:07

    For django < 1.10

    topic._default_manager.get(id=topic_id)
    

    Though you should not use it like this. The _default_manager and _base_manager are private, so it's recomended to use them only if you're inside the Topic model, like when you want to use the Manager in a proprietary function let's say:

    class Topic(Model):
    .
    .
    .
        def related(self)
            "Returns the topics with similar starting names"
            return self._default_manager.filter(name__startswith=self.name)
    
    topic.related() #topic 'Milan wins' is related to:
    # ['Milan wins','Milan wins championship', 'Milan wins by one goal', ...]
    

提交回复
热议问题