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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:17:31+00:00 2026-06-03T10:17:31+00:00

I have a model similar to this one: class MyModel(models.Model): name = models.CharField(max_length =

  • 0

I have a model similar to this one:

class MyModel(models.Model):
    name = models.CharField(max_length = 30)
    a    = models.ForeignKey(External)
    b    = models.ForeignKey(External, related_name='MyModels_a')

    def __unicode__(self):
        return self.a + self.b.name + self.b.name

So when I query it I get something like this:

>>> MyModel.objects.all()
[<MyModel: Name1AB>,<MyModel: Name2AC>,<MyModel: Name3CB>,<MyModel: Name4BA>,<MyModel: Name5BA>]

And I’d like to represent this data similar to the following.

[[ []                 , [Name1AB] , [Name2AC] ]
 [ [Name4BA, Name5BA] , []        , []        ]
 [ []                 , [Name3CB] , []        ]]

As you can see the rows would be ‘a’ in the model; and the columns would be ‘b’
I can do this, but it takes a long of time because in the real database I have a lot of data. I’d like to know if there’s a Django built in way to do this.

I’m doing it like this:

mymodel_list  = MyModel.objects.all()
external_list = External.objects.all()

for i in external_list:
    for j in external_list:

        print(mymodel_list.filter(a=i).filter(arrl=j).all(),end='')
        print()

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-03T10:17:32+00:00Added an answer on June 3, 2026 at 10:17 am

    Three ways of doing it but you will have to research a bit more. The third option one may be the most suitable for what you are looking for.

    1) Django queries

    The reason it is taking a long time is because you are constantly accessing the database in this line:

    print(mymodel_list.filter(a=i).filter(arrl=j).all(),end='')
    

    You may have to start reading what the Django documentation say about the way of working with queries. For what you are doing you have to create the algorithm to avoid the filters. Using MyModel.objects.order_by(‘a’) may help you to build a efficient algorithm.

    2) {% ifchanged …%} tag

    I suppose you are using print to post your answer but you probably need it in html. In that case, you may want to read about the ifchanged tag. It will allow you to build your matrix in html with just one db access.

    3) Many to many relations

    It seems you are sort of simulating a many to many relation in a very peculiar way. Django has support for many to many relations. You will need an extra field, so you will also have to read this.

    Finally, for doing what you are trying with just one access to the database, you will need to read prefetch_related

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

Sidebar

Related Questions

I have a Project model similar to: class Project(models.Model): ... lead_analyst=models.ForeignKey(User, related_name='lead_analyst') ... I
I have few similar models in Django: class Material(models.Model): title = models.CharField(max_length=255) class Meta:
I have a model similar to this: public class myModel { public ClassA ObjectA
I have a class Similar to this public class Model { public TimeSpan Time1
I have a model with a ManyToManyField similar to this one (the model Word
I have model: # encoding: utf-8 class Tag include Mongoid::Document field :name, type: String
I have a Database similar to the one in this example . Not the
I have the following Django models: class Contact(models.Model): id #primary key #Other contact info
I have two Django model classes that are structured similar to the following: class
I have a Java model similar to: public class Country { @Id private String

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.