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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:31:10+00:00 2026-05-18T08:31:10+00:00

Given django models A and B: class A(models.Model): things = models.ManyToManyField(‘B’) class B(models.Model): score

  • 0

Given django models A and B:

class A(models.Model):
    things = models.ManyToManyField('B')

class B(models.Model):
    score = models.FloatField()
    text = models.CharField(max_length=100)

How do I get a list of A entities ordered by the text of the highest-scoring B in things?

  • 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-18T08:31:11+00:00Added an answer on May 18, 2026 at 8:31 am

    If I understand you correctly, this should do it. list will contain a list of all the objects of model A sorted by the text of each object’s highest scoring thing.

    dict = {}
    list = []
    for a in A.objects.all():
        dict[a] = a.things.all().order_by("-score")[0].text
    for k, v in sorted(dict.items(), key=lambda x: x[1]):
        list.append(k)
    

    There might be a prettier way to write it, but I can’t think of one off the top of my head…

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

Sidebar

Related Questions

Given: from django.db import models class Food(models.Model): Food, by name. name = models.CharField(max_length=25) class
Given a class: from django.db import models class Person(models.Model): name = models.CharField(max_length=20) Is it
I have the following Django 1.2 models: class Category(models.Model): name = models.CharField(max_length=255) class Article(models.Model):
My models: class Contact(models.Model): first_name = models.CharField(_(First name), max_length=30, ) last_name = models.CharField(_(Last name),
I'm struggling with the design of a django application. Given the following models: class
In Django, given excerpts from an application animals likeso: A animals/models.py with: from django.db
I have a blog-like application with stories and categories: class Category(models.Model): ... class Story(models.Model):
I have a model like this: class Person(models.Model,Subject): name = .. The class Subject
I have the following models: class Post(models.Model): message = models.TextField() (etc.) class UserProfile(models.Model): user
Given a Django model, I'm trying to list all of its fields. I've seen

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.