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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:30:19+00:00 2026-06-18T11:30:19+00:00

I have two tables of data in a Django app, one containing shipping vessel

  • 0

I have two tables of data in a Django app, one containing shipping vessel details and the other containing offers related to individual vessels. My models are as follows:

class Vessel(models.Model):
    name = models.CharField(max_length=64)
    image = models.CharField(max_length=64)
    vesseltype = models.ForeignKey(VesselType)
    capacity = models.IntegerField()
    length = models.FloatField()
    beam = models.FloatField()
    speed = models.IntegerField()
    activities = models.CharField(max_length=256)
    category = models.IntegerField()
    display_order = models.IntegerField()
    published = models.BooleanField()
    def __unicode__(self):
        return self.name

class Offer(models.Model):
    vessel = models.ForeignKey(Vessel)
    description = models.CharField(max_length=128)
    date = models.DateField()
    duration = models.IntegerField()
    itinerary = models.TextField()
    price = models.IntegerField()
    display_order = models.IntegerField()
    link = models.URLField()
    published = models.BooleanField()
    def __unicode__(self):
        return self.description

In my views.py I have the following code:

def home(request):
    vessels = Vessel.objects.filter(published='y').order_by('display_order')
    offers = Offer.objects.filter(vessel__published__exact='y').filter(published='y').order_by('display_order')
    t = loader.get_template('index.html')
    c = Context({
        'vessels' : vessels,
        'offers' : offers,
    })

In the template I need to display multiple divs each containing the vessel name, some of the vessel details and any number of offers related to that vessel.

I’ve looked at queries trying to link the offers in with each vessel and I’ve also tried identifying the vessel name inside loop code in the template to just display an offer if the vessel.id and offer.vessel match up.

So far, nothing has been successful with just getting errors on the query side and separate lists of vessel names and offers by going the template way. How do I join this information so I could just use something like this in the template:

for offer in offers:
    offer.vessel.name
    offer.vessel.category
    offer.description
    offer.price

or am I expecting too much? Been at this for several hours and can’t find a solution in the docs but it’s probably obvious. Changing the query, the view code or the template is not a problem.

  • 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-18T11:30:20+00:00Added an answer on June 18, 2026 at 11:30 am

    The answer is to change your view code. You want a queryset like Offer.objects.all().select_related(). select_related will make sure you don’t hit the database every time your loop executes because it will follow foreign keys to the end before executing a query.

    You’re also using the wrong template syntax. It needs to be something like:

    {% for object in object_list %}
    {{ object.attribute }}
    {% endfor %}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have two sets of data (two tables) for patient records, one 1999-2003, the other
i have two data tables with the same structure the first one has one
I have two tables, one has about 1500 records and the other has about
I have two MySQL tables describing data that can be extended into subclasses, one
I have two tables where the data is not related For each row in
I have two data tables. One contains all of users identifiable informaiton (username, id,
I have two Django models with are related to each other through a many-to-many
I have two tables with related data, and I want to select all the
I am using C#(asp.net). I have two tables(data and details) in a same database.
I have two tables containing data that are essentially headline lists from different sources.

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.