Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6598709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:23:17+00:00 2026-05-25T18:23:17+00:00

I developing a custom manager class with chainable method. Got a problem. I need

  • 0

I developing a custom manager class with chainable method. Got a problem. I need to randomize filtered query. To get a random record I need a count of filtered and distinct records. But I don’t know how to get it. On the contrary, I have a count of all records.

class RandomQueryset(models.query.QuerySet):

    def randomize(self):        
        count = self.aggregate(count=Count('id'))['count']
        random_index = random.randint(0, count - 1)
        return self.all()[random_index]    


class RandomManager(models.Manager):

    def get_query_set(self):
        return RandomQueryset(self.model, using=self._db)

    def randomize(self):
        return self.get_query_set().randomize()

Using:

>>> posts = PostPages.random_objects.filter(image_gallery__isnull=False).distinct()

>>> posts.randomize()

Sooner or later I get an error because that count exceeds the number of records in the current query.

IndexError: list index out of range
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:23:18+00:00Added an answer on May 25, 2026 at 6:23 pm

    You’ve asked me to post one of my questions as an answer, so here goes.

    This looks like you need to use Django’s built-in count() function docs.djangoproject.com/en/dev/ref/models/querysets

    I do not see its use in your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a custom autocomplete control in pure WinApi, and the problem that I've
I am developing a custom shopping cart for which existing customers will need to
I'm currently developing a custom control that derives from CStatic MFC class (Smart Device
We are developing a custom textual DSL, that will ultimately get integrated into Visual
I'm developing a few custom ant tasks that all need to initialize the same
I am developing custom list field using Table Row Manager in blackberry.it displays list
I'm developing a simple custom Drupal Module for creating Quizz, and I need some
Does anyone have any good links for developing custom field and content types that
I am developing a custom control derived from System.Windows.Controls.ContentControl. In the controls default template
I'm developing a custom control, which most resembles a text area. I am drawing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.