Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use custom manager with related objects?

I have a custom manager. I want to use it for related objects. I found use_for_related_fields in docs. But it does not work the way I used it:

class RandomQueryset(models.query.QuerySet):      def randomize(self):                count = self.count()         random_index = random.randint(0, count - 1)         return self.all()[random_index]   class RandomManager(models.Manager):      use_for_related_fields = True      def get_query_set(self):         return RandomQueryset(self.model, using=self._db)      def randomize(self):         return self.get_query_set().randomize() 

I used it for one model:

>>> post = PostPages.default_manager.filter(image_gallery__isnull=False).distinct().randomize() 

And tried to do the same with m2m related object:

>>> post.image_gallery.randomize() 

Got an error:

AttributeError: 'ManyRelatedManager' object has no attribute 'randomize' 

Is it possible to use a custom manager in the way I did it? If so, how do you make it work?

Edit

My models:

class ShivaImage(models.Model, ImageResizing):     image = models.ImageField(upload_to='img')     slide_show = models.BooleanField()      title = models.CharField(max_length=100)     text = models.TextField(max_length=400)     ordering = models.IntegerField(blank=True, null=True)      objects = RandomManager()   class PostPages(models.Model):     image_gallery = models.ManyToManyField(ShivaImage, blank=True,                                        related_name='gallery',)     # all the other fields...       objects = RandomManager() 
like image 268
I159 Avatar asked Sep 20 '11 18:09

I159


People also ask

How do I use custom Manager in Django?

Custom managers. You can use a custom Manager in a particular model by extending the base Manager class and instantiating your custom Manager in your model. There are two reasons you might want to customize a Manager : to add extra Manager methods, and/or to modify the initial QuerySet the Manager returns.

How do I override Queryset?

To override default queryset in Python Django admin, we can override the get_queryset method in our model. to add the get_queryset into MyModelAdmin . In it, we call call the super constructor with MyModelAdmin and self . And we get the queryset wirth get_queryset from the super class.

What is objects model manager?

25 Nov 2021 • 4 min read. Django Model Manager is the interface through which database query operations are provided to Django models. objects is the default model manager and each model needs to have at least one model manager.

What is the use of model manager in Django?

In other words, in a Django model, the manager is the interface that interacts with the database. For example, when you want to retrieve objects from your database, you need to construct a QuerySet via a Manager on your model class. By default, the manager is available through the Model.


1 Answers

For completeness of the topic, Django 1.7 (finally) supports using a custom reverse manager, so you can do something like that (just copying from the django docs):

from django.db import models  class Entry(models.Model):     objects = models.Manager()  # Default Manager     entries = EntryManager()    # Custom Manager  b = Blog.objects.get(id=1) b.entry_set(manager='entries').all() 
like image 140
Serafeim Avatar answered Sep 30 '22 20:09

Serafeim