Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing other tables in template
#1
Hi there,
I'm really stuck. I have a site for tracking projects and while accessing the project model is simple enough, I can't figure out how to access a second table (Update) in the template.
Here is my model:
from datetime import datetime
from django.contrib.auth.models import Permission, User
from django.db import models
from django.urls import reverse

class Area(models.Model):
    area_name = models.CharField(max_length=45,unique=True)
    area_code = models.CharField(max_length=45,unique=True)

    def __str__(self):
        return self.area_name

class Priority(models.Model):
    priority = models.CharField(max_length=16, unique=True)
    colour = models.CharField(max_length=7, unique=True)

    def __str__(self):
        return self.priority


class Project(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    user = models.ForeignKey(User, on_delete=models.PROTECT)
    area_id = models.ForeignKey(Area, on_delete=models.PROTECT)
    title = models.CharField(max_length=128, unique=True)
    summary = models.CharField(max_length=256)
    others = models.CharField(max_length=128, blank=True)
    deadline = models.DateField(blank=True)
    priority = models.ForeignKey(Priority, on_delete=models.PROTECT)
    closed = models.DateTimeField(blank=True,null=True)

    def __str__(self):
        return self.title


class UpdateCategory(models.Model):
    cat_name = models.CharField(max_length=24,unique=True)

    def __str__(self):
        return self.cat_name


class Update(models.Model):
    p_id = models.ForeignKey(Project, on_delete=models.PROTECT)
    category = models.ForeignKey(UpdateCategory, on_delete=models.PROTECT)
    update = models.TextField(max_length=2048)
    date_added = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.update
here is my view:
from django.contrib.auth import authenticate, login
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.core.files.storage import FileSystemStorage
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import render, redirect, get_object_or_404
from django.template import RequestContext
from django.urls import reverse
from django.views.generic import (
    CreateView,
    DetailView,
    DeleteView,
    ListView,
    TemplateView,
    UpdateView
)
from .forms import ProjectModelForm
from .models import (
    Project,
    Area,
    Priority,
    Update,
    UpdateCategory
)

class ProjectView(ListView):
    template_name = 'project_portal/sidev.html'
    queryset = Project.objects.all()

def get_context_data(self, **kwargs):
    context = super(ProjectView, self).get_context_data(**kwargs)
    context['p_model'] = Project.objects.all()
    context['u_model'] = Update.objects.all()
    return context
here is my template:
<!DOCTYPE html>
{% load staticfiles %}
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Project Portal :: sidev</title>
    <link rel="stylesheet" href="{% static 'project_portal/css/master.css' %}">
    <link rel="shortcut icon" href="{% static 'project_portal/images/favicon.ico' type='image/x-icon' %}">
  </head>
  <body>
    <div id="center">

      <header>
        <div class="navFixedLogo">
          <img src="{% static 'project_portal/images/index.png' %}" alt="Sky Projects Portal Logo" height="48px">
        </div>
        <div class="navFixedRight">
          <img src="{% static 'project_portal/images/icon_add_circle.png' %}" alt="Add" width="32">
        </div>
      </header>

      <div id="main">
        <div id="content">
          <hgroup>
            <h1>OTT</h1>
            <h2>Service Integration Development Project Portal</h2>
          </hgroup
        </div>
        <div class="container">
          {% if object_list %}
          {% for item in object_list %}

          <div class="box1">
            <h4>{{ item.title }}</h4>
            <p>Project Status</p>
            <div class="square"></div>
          </div>

          <div class="box2">
            <strong>{{ item.summary }}</strong>
          </div>

          <div class="box3">
            <strong>{{ item.user }}</strong>
          </div>

          <div class="box4">
            <table class="items">
              <col width="160px">
              <tr>
                <td>Monitoring Status</td>
                <td>Impact Scenario</td>
                <td>&#x2705;</td>
              </tr>
              <tr>
                <td>Support Model</td>
                <td>&#x2705;</td>
              </tr>
              <tr>
                <td>Impact Scenarios</td>
                <td>&#x2705;</td>
              </tr>
              <tr>
                <td>Training</td>
                <td>&#x2705;</td>
              </tr>
              <tr>
                <td>Service Rehearsals</td>
                <td></td>
              </tr>
              <tr>
                <td>Support Documentation</td>
                <td></td>
              </tr>
              <tr>
                <td>Other Updates</td>
                <td></td>
              </tr>
            </table>
          </div>

          <div class="box5">
            <table>
              <tr>
                <td>{{ Update data here }}</td>
              </tr>
              <tr>
                <td>{{ Update data here }}</td>
              </tr>
              <tr>
                <td>{{ Update data here }}</td>
              </tr>
              <tr>
                <td>{{ Update data here }}</td>
              </tr>
              <tr>
                <td>{{ Update data here }}</td>
              </tr>
              <tr>
                <td>{{ Update data here }}</td>
              </tr>
              <tr>
                <td>{{ Update data here }}</td>
              </tr>

            </table>

          </div>
          <div class="box6">
            <h4>Project Timeline</h4>
          </div>
          <div class="box7">TimeLine</div>
          {% endfor %}
          {% endif %}
        </div>
      </div>
    </div>
  </body>
</html>
I can't work out how to access the data from the Update model. I am after the last update from each update category, but I'm lost and can't figure it out from the documentation. There is a reference to "Project.update_set.all()", but I don't understand how that works as I need to access all the data from the Project Model (this all works) and then access the data from the update model as well. Can someone point out what I am missing. It that means redesigning the whole database then so be it.
Reply
#2
Is this not possible?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020