Friday, January 3, 2014

Filtering out results in Automatic Managers

Hi,

"A manager that filters results in get_queryset() is not appropriate for use as an automatic manager."

Can you please tell me why is it not appropriate because this is exactly what I want, filter results in backward RelatedManagers?

It is written, "If you override the get_queryset() method and filter out any rows, Django will return incorrect results.". What type of incorrect results will Django return?

Thank you


Example:

class TrashableManager(models.Manager):
    use_for_related_fields = True
    def get_query_set(self):
        return super(TrashableManager, self).get_query_set().filter(trashed=False)

class TrashedManager(models.Manager):
    def get_query_set(self):
        return super(TrashableManager, self).get_query_set().filter(trashed=True)

class Trashable(models.Model):
    trashed = models.BooleanField(default=False)
    datetime_trashed = models.DateTimeField(blank=True, null=True)
    objects = TrashableManager()
    trashed_objects = TrashedManager()

    def trash(self):
        if not self.trashed:
            self.datetime_trashed = datetime.now()
        self.trashed = True
        self.save()

    class Meta:
        abstract = True

--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c01696d6-f1bd-43a5-8fea-922d27471738%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment