Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: How do you access a model's instance from inside a manager?

class SupercalifragilisticexpialidociousManager(models.Manager):
    # Sorry, I'm sick of Foo and Spam for now.
    def get_query_set(self, account=None):
        return super(SupercalifragilisticexpialidociousManager,
                     self).get_query_set().filter(uncle=model_thats_using_this_manager_instance.uncle)

The magic I'm looking for is the "uncle=model_thats_using_this_manager_instance.uncle". It seems like I should be able to do this somehow. I know I could say self.model to get the model, but how to get the instance?

like image 860
orokusaki Avatar asked Sep 17 '25 20:09

orokusaki


1 Answers

It doesn't make sense to ask for an instance when you're using a manager. Managers are class-level attributes - if you try and do foo.objects.all() where foo is an instance of Supercalifragilisticexpialidocious, you will explicitly get an error:

AttributeError: Manager isn't accessible via Supercalifragilisticexpialidocious instances
like image 159
Daniel Roseman Avatar answered Sep 19 '25 10:09

Daniel Roseman