Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing the Foreign Key value in Django template

Here is my issue. I am new to python/django (about 2 months in). I have 2 tables, Project and Status. I have a foreign key pointing from status to project, and I am looking to try to display the value of the foreign key (status) on my project template, instead of the address of the foreign key.

Here is my models.py

from django.db import models
from clients.models import Clients
from django.contrib.auth.models import User
from settings import STATUS_CHOICES

from django.db import models
from clients.models import Clients
from django.contrib.auth.models import User
from settings import STATUS_CHOICES

class Project(models.Model):
   client = models.ForeignKey(Clients, related_name='projects')
   created_by = models.ForeignKey(User, related_name='created_by')


#general information
   proj_name = models.CharField(max_length=255, verbose_name='Project Name')
   pre_quote = models.CharField(max_length=3,default='10-')
   quote = models.IntegerField(max_length=10, verbose_name='Quote #', unique=True)
   desc = models.TextField(verbose_name='Description')
   starts_on = models.DateField(verbose_name='Start Date')
   completed_on = models.DateField(verbose_name='Finished On')

   def __unicode__(self):
      return u'%s' % (self.proj_name)   


class Status(models.Model):
  project = models.ForeignKey(Project, related_name='status')
  value = models.CharField(max_length=20, choices=STATUS_CHOICES,                     verbose_name='Status')
  date_created= models.DateTimeField(auto_now=True) 

  def __unicode__(self):
     return self.value

  class Meta:
      verbose_name = ('Status')
      verbose_name_plural = ("Status")

My views.py

@login_required
 def addProject(request):
 if request.method == 'POST':
      form = AddSingleProjectForm(request.POST)
      if form.is_valid():
        project = form.save(commit=False)
        project.created_by = request.user  
        project.save()
        project.status.create(
                value = form.cleaned_data.get('status', None)
        )            
        return HttpResponseRedirect('/project/')
 else:
    form = AddSingleProjectForm()

 return render_to_response('project/addProject.html', {
 'form': form, 'user':request.user}, context_instance=RequestContext(request))

And finally my template:

{% if project_list %}
<table id="plist">
    <tr id="plist">
        <th>Quote #</th>
        <th>Customer</th>
        <th>Date</th>
        <th>Project Name</th>
        <th>Status</th>
        <th>Contact</th>
    </tr id="plist">
    {% for p in project_list %}
    <tr id="plist">
        <td><a href="/project/{{ p.id }}/view">{{ p.pre_quote }}{{ p.quote }}</a></td>
        <td>{{ p.client }}</td>
        <td>{{ p.starts_on }}</td>
        <td>{{ p.proj_name }}</td>
        <td>{{ p.status_set.select_related }}</td>
    <td>{{ p.created_by }}</td>
    </tr>
     {% endfor %}
    </table>

{% else %}
    <p>No projects available.</p>
{% endif %}

Any help would be much appreciated. Thank you!

like image 798
TheLifeOfSteve Avatar asked Oct 19 '10 14:10

TheLifeOfSteve


1 Answers

I'm guessing you mean here:

<td>{{ p.status_set.select_related }}</td>

This doesn't do anything. select_related is an optimisation feature, it has nothing to do with actually getting or displaying the related content. If you want to do that, you will have to iterate through the result of p.status_set.all.

like image 189
Daniel Roseman Avatar answered Oct 25 '22 14:10

Daniel Roseman