When it make sense to use a different name for a custom manager of the Django`s model?

28 views
Skip to first unread message

Seti Volkylany

unread,
Jul 23, 2016, 6:32:32 AM7/23/16
to Django users
If why know good article about it, send me link, I will resolve of itself.

Derek

unread,
Jul 23, 2016, 10:48:33 AM7/23/16
to Django users
https://docs.djangoproject.com/en/1.9/topics/db/managers/#modifying-a-manager-s-initial-queryset

Note the difference between overriding the default queryset (called "objects" by Django) vs creating your own - in the example shown, it is called "dahl_objects" (but of course should be a name that makes sense for your app).

Seti Volkylany

unread,
Jul 23, 2016, 10:58:09 AM7/23/16
to django...@googlegroups.com
Sorry, Derek but I am do not understand your answer.

"(but of course should be a name that makes sense for your app)" - Why? I was completely satisfied with default manager`s name - "objects"

In my project, I am using next code:

class Poll(models.Model):
[fields and class Meta]
objects = models.Manager() objects = PollManager.from_queryset(PollQuerySet)()

[other code]

where PollManager is my custom manager, PollQuerySet - my custom QuerySet for the model Poll

If I follow your`s advice then must next:

class Poll(models.Model):
[fields and class Meta]
objects = models.Manager() polls = PollManager.from_queryset(PollQuerySet)()

[other code]




--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/Oqq48vVqVFM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/415c3f1c-4d3d-41f5-b2e5-49c3b7db7e1e%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Derek

unread,
Jul 24, 2016, 12:58:15 PM7/24/16
to Django users
If you read the docs, it is explained.

Unless there is good reason to, you should keep the default "objects" code as is, and make up a name for your own, new, custom manager.  If you don't, and call your "objects", then you must realise you overriding the way that Django allows you to get all data back for a model.

This code of yours does not make sense:

    objects = PollManager.from_queryset(PollQuerySet)()

You need to create something like:

class MinePollManager(models.Manager):
    def get_queryset(self):
        return super(MinePollManager, self).get_queryset().filter(code='mine')

and then in your model:
      
    objects = models.Manager()  # the default manager
    mine = MinePollManager()  # choose name that is appropriate for your app/use

Hope this clarifies.
Reply all
Reply to author
Forward
0 new messages