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

  • Home
  • SEARCH
  • 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 8060521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:01:10+00:00 2026-06-05T10:01:10+00:00

In straight Django, you can access random model instances by: randinst = MyModel.objects.order_by(‘?’) Note:

  • 0

In straight Django, you can access random model instances by:

randinst = MyModel.objects.order_by('?')

Note: Though there are performance issues with this, I have tested with the sqlite backend and I do get really random results for up to 100000 tries. Since my app does not require significant performance beyond this, I am not concerned about other backends.

What I wish to accomplish is this: A client makes a request, /api/v1/mymodel/?limit=10, and gets a random set of ten rows from MyModel via tastypie just like you would get running the above snippet 10 times. It then makes the same request, and receives 10 different (within the limits of probability) random rows.

Note: I have tried requesting /api/v1/mymodel/?ordering='?' and all resonable variants thereof to no avail. Also unhelpful is setting MyModelResource.Meta.ordering = ['?']

Is there any way to accomplish my goal with tastypie? Are there other solutions to try? Thanks.

  • 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-06-05T10:01:11+00:00Added an answer on June 5, 2026 at 10:01 am

    Answer courtesy of on #tastypie.

    Set the queryset of the model as follows:

    class MyModelResource(ModelResource):
      class Meta:
        queryset = MyModel.objects.all().order_by('?')
    

    The key here is to use objects.all().order_by not just objects.order_by.

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

Sidebar

Related Questions

I'm writing a form with Django. The form is a model form for a
Pretty straight forward question, but I can't find a way to do it. When
Pretty straight forward, I just can't get it to work. What can I do
Essentially, I am doing some of the development for my Django app straight on
I'm using django-tables (http://pypi.python.org/pypi/django-tables/0.2) to render the contents of a MySQL table. The model
I have two models in Django like follows(in pseudo code) class Medicine(db.Model): field_1 =
I've got a Django model with a self-referencing foreign key, so my model (as
I am getting an Indention Error. This code is straight from the django first
I am currently using Django to construct JSON encoded objects which a retrieved using
Straight to the point: Where can I place the algoritm for password hash encryption

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.