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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:00:08+00:00 2026-06-07T13:00:08+00:00

I have a Contact class that can have many Conversations. But each conversation can

  • 0

I have a Contact class that can have many Conversations. But each conversation can belong to a single Contact.
So its a One-To-Many relationship.

class Contact(models.Model):
    first_name          = models.CharField()
    last_name           = models.CharField()   

class Conversation(models.Model):
    contact             = models.ForeignKey(Contact)
    notes               = models.TextField()

    def __unicode__(self):
        return self.notes

Now when I pass in the contacts to the template, I would like to have one field that shows the last conversation for the contact.

contacts= profile.company.contact_set.all()[:10]
calls = []

for c in contacts:
    calls.append(max(c.conversation_set.all()))

And I pass them in like this:

vars = {'contacts' : contacts, 'calls': calls}
variables = RequestContext(request, vars)
return render_to_response('main_page.html', variables)

In the template I came up with this magic:

{% for idx, val in contacts %}
                    <tr>
                        <td>...
                        </td>
                        <td>    
                            {% if calls %}  
                                 {{ calls.idx }} 
                            {% endif %}</td>
                    </tr>
{% endfor %}

This doesn’t show anything for calls. But if I replaced calls.idx with calls.0 I get the first one displayed.

What am I doing wrong? Beside the fact that it could be done probably much easier than that. 🙂 I am open for better ways of doing it.

  • 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-07T13:00:10+00:00Added an answer on June 7, 2026 at 1:00 pm

    You can’t do this sort of indirect lookup in the template language – calls.idx will always refer to an attribute idx belonging to calls.

    However I think a better solution is to add the call value directly onto the contact object – don’t forget that in Python objects are dynamic, so you can annotate anything you like onto them.

    for c in contacts:
        c.max_call = max(c.conversation_set.all())
    

    As an aside, does that max really work? I’m not sure what it would be doing the comparison based on. An alternative is to define get_latest_by in your Conversation model, then you can avoid this loop altogether and just call {{ contact.conversation_set.get_latest }} in your template for each contact through the loop.

    (Note this will still be pretty inefficient, there are various ways of getting the whole set of max conversations in one go, but it will do for now.)

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

Sidebar

Related Questions

I have a Contact Domain class that can be associated with multiple Organizations, which
I have a working model, but have noticed that the relationship has been created
I have a class that collects a series of strings (contact's names) and I
I have two contact forms in my CakePHP application -- one with its own
I have a 'Contact' class with two properties : firstName and lastName. When I
I have a class Contact (base class),a class called Customer and a class called
I have two models: class Contact(models.Model): name = models.CharField(max_length=255) class Campaign(models.Model): contact = models.ForeignKey(Contact,
I have the following model: public class Contact { public Contact() { Name =
I have created the following class class Contact def initialize(id, name, phone) @id =
I have the following models set up: class Contact < ActiveRecord::Base belongs_to :band belongs_to

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.