Sunday, July 24, 2016

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

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.

On Saturday, 23 July 2016 16:58:09 UTC+2, Seti Volkylany wrote:
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]




On Sat, Jul 23, 2016 at 5:48 PM, Derek <game...@gmail.com> wrote:
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).

On Saturday, 23 July 2016 12:32:32 UTC+2, Seti Volkylany wrote:
If why know good article about it, send me link, I will resolve of itself.

--
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.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/bf4d2588-b37d-40dd-b1bc-2fd505698c49%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment