views:

36

answers:

2

Hello everyone.

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!

+2  A: 

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.

Daniel Roseman
Yes that spot exactly. So are you saying I need to loop through p.status_set.all ? Sorry Im still new to this.
Steve
Yes, because you have multiple statuses for each `p`. If that's not what you want, your model structure might be wrong.
Daniel Roseman
thank you. I have got it working
Steve
A: 

In your model, you have defined the related name for this ForeignKey as "status." Thus, you can now use "status" as this name instead of the "_set" business.

Since this is a ForeignKey (ManyToOne) field, you can't simply display the field as if there were only one value. Instead you need .all, which will return a queryset of all the statuses that point to the object in question. You can then iterate through these.

If instead, you know that each project will have only one status, you can use a OneToOne field instead of a ForeignKey.

Justin Myles Holmes