3
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?

orokusaki
  • 55,146
  • 59
  • 179
  • 257

4 Answers4

7

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
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thanks Daniel. You're example helped me to understand the context in which managers exist. I still need to find a way to better handle this situation application-wide vs having to write special code hither and thither. – orokusaki Jan 26 '10 at 00:07
4

As far as I know, you cannot access the model from inside a manager. It doesn't make sense as managers operate on the whole table.

You should do something like this in the model:

class Model(models.Model):
    # some attributes here
    def getAllRelativesWithSameUncle(self):
        return Model.objects.filter(uncle = self.uncle)

or in the manager:

class SupercalifragilisticexpialidociousManager(models.Manager):
    def getSelfRelativesFor(self, model):
        return self.get_queryset().filter(uncle=model)
mrmuggles
  • 2,081
  • 4
  • 25
  • 44
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
3

In methods like object.related_name.create() under the hood the Djangos sends a hint argument:

class UserQuerySet(QuerySet):
    def create(self, *args, **kwargs):
        print(self._hints)
        # >>> {'instance': <User: random-user>}
        print(self._hints.get('instance'))
        # >>> <User: random-user>

I'm using Django 1.11 nowadays.

Luan Fonseca
  • 1,467
  • 1
  • 13
  • 22
0

try self.model.

From the Docs

"Another thing to note is that Manager methods can access self.model to get the model class to which they’re attached." — https://docs.djangoproject.com/en/4.2/topics/db/managers/#adding-extra-manager-methods

What led to the answer: The manager knows what model to create when you call .create() from the manager.

Jacob Valenta
  • 6,659
  • 8
  • 31
  • 42