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 9029311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:13:15+00:00 2026-06-16T07:13:15+00:00

Here’s my model design: class Group(models.Model): name = models.CharField(max_length=30) … class User(models.Model): name =

  • 0

Here’s my model design:

class Group(models.Model):
    name  = models.CharField(max_length=30)
    ...

class User(models.Model):
    name              = models.CharField(max_length=50)
    ...
    group             = models.ForeignKey(Group)

class Photo(models.Model):
    title                = models.CharField(max_length=100)
    ...
    user                 = models.ForeignKey(User)

How can I get a list of all groups, under each group 5 random photos which belong to users belonging to the group?

I tried:

g = Group.objects.all()

for group in g:
    r = Photo.objects.filter(user.group=group).order_by('?')[:5]

That is giving:

Django Version: 1.4.3
Exception Type: SyntaxError
Exception Value:    
keyword can't be an expression (views.py, line 37)
Exception Location: /PATH/python2.7/site-packages/django/utils/importlib.py in import_module, line 35
Python Executable:  /PATH/python
Python Version: 2.7.3

But even if my code worked, I don’t believe it’s the ideal way since it will generate too many queries.

Please help.

  • 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-16T07:13:17+00:00Added an answer on June 16, 2026 at 7:13 am

    The issue is that you can’t use user.groups as a keyword argument to the .filter(…) function.

    Instead, use user__groups=group to span the user -> group relationship:

    Photo.objects.filter(user__group=group).order_by('?')[:5]
    

    The relevant documentation: https://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships

    Unfortunately getting rid of the for-loop would be fair fairly tricky, as it would require querying for the top N rows from a GROUP BY query… So unless you’ve got a bunch of groups, the Python for-loop is probably the most straight forward way to do it.

    Now, that said, if performance is important, you’ll probably need to come up with a smarter solution because ordering by RANDOM() is very slow too…

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

Sidebar

Related Questions

Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Here's some sample HTML <style> .icon {display:none;} </style> <ul> <li>ABC <i id=abc class=icon>x</i></li> <li>DEF
Here is my AsyncTask. Getting exception in the AsyncTask.. private class GetCategories extends AsyncTask<String,
Here is a scenario: User installs .NET application that you have made. After some
Here is my query: select word_id, count(sentence_id) from sentence_word group by word_id having count(sentence_id)
Here's my button element: <button type=submit class=btn-submit><span><strong>Let Me In, please!</strong></span></button> and here's the CSS:
Here's my database: id name parent_id 1 Computers NULL 2 Apple 1 3 Books
Here is my text file forms. S1,F2 title including several white spaces (abbr) single,Here<->There,reply
Here's the problem....I have three components...A Page that contains a User Control and a

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.