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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:54:46+00:00 2026-06-03T07:54:46+00:00

I have a parent model defined as : class PurchaseOrder(models.Model): number = models.CharField(max_length=30, unique=True)

  • 0

I have a parent model defined as :

class PurchaseOrder(models.Model):
    number =  models.CharField(max_length=30, unique=True)
    def services(self):
       return  self.service_set.filter(purchaseorder=self.pk)

Child model Service is defined as :

class Service(models.Model):
    purchaseorder = models.ForeignKey(PurchaseOrder)
    modules = models.ManyToManyField(Module)

    def get_modules(self):
        return self.modules.all()

There is another model Module having M2M relation with the Service model. I wish to display the modules as a list in the Parent model change_list view i.e in list_display list. How do I do that? What is the effect on the number of database hits?

  • 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-03T07:54:47+00:00Added an answer on June 3, 2026 at 7:54 am

    Using your example, I do something like this

    class PurchaseOrder(models.Model):
        number = models.CharField(max_length=30, unique=True)
    
        def get_modules(self):
            return ', '.join([module.FIELD for service.modules.all() in self.service_set.filter(purchaseorder=self.pk) for module in service.modules.all()])
    

    then in admin.py

    class PurchaseAdmin(admin.ModelAdmin):
        list_display = ('number', 'get_modules')
    
    admin.site.register(Service, ServiceAdmin)
    

    I’m not sure about the db hits but you can use a tool like django-debug-toolbar to see the SQL queries generated before and after you implement it.

    UPDATED BASED ON COMMENT

    Try the above list comprehension and see if it works? You might need to play around with the output a bit to get it right in shell.

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

Sidebar

Related Questions

I have a Model with a Foreign Key of Parent class Item(models.Model): parent =
I have a parent model that has many child models, for example: App.Blog =
I have a model that looks something like this: public abstract class Parent {
I have the following two models: class Parent < ActiveRecord::Base has_one :child, dependent: :destroy
I have the following subclass: class UserProfile(User): user = models.OneToOneField(User, parent_link=True) And an UserProfileAdmin:
So I have defined a model like this: class Model extends Zend_Db_Table_Abstract { $_primary
I have a model defined as follows: public class Link extends Model { @Required
I have a View Model that is defined as follows: public class VariableViewModel {
im have model Category. class Category < ActiveRecord::Base has_ancestry :cache_depth => true, :depth_cache_column =>
I have transactional Groups of entities of type db.Model that have a parent defined.

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.