views:

208

answers:

2

Hi to all!

Just begin my Python/Django experience and i have a problem :-)

So i have a model.py like this:

from django.db import models

class Priority(models.Model):
    name = models.CharField(max_length=100)

class Projects(models.Model):
    name = models.CharField(max_length=30)
    description = models.CharField(max_length=150)
    priority = models.ForeignKey(Priority)

class Tasks(models.Model):
    name = models.CharField(max_length=30)
    description = models.CharField(max_length=40)
    priority = models.ForeignKey(Priority)

In the priority table i plan to store data like 1.High, 2.Medium , 3.Low and in Tasks table priority will be stored as id (1, 2 or 3)

And the question is how to write a view that display all my tasks but with Priority named? For example:

name: Task 1

description: Description 1

priority: **High**
A: 

There are many ways of accomplishing what you need. One of the easiest would be to keep a dictionary of number to string. Like this: 1->High, 2->Medium, 3->High.

Keep this dictionary outside of your view functions so that you can access it from any of your view functions that need to get the priority.

You could also simply write a switch that determines what to display in the template.

AlbertoPL
+1  A: 

Your view doesn't have to do much.

tasks = Tasks.objects.all()

Provide this to your template.

Your template can then do something like the following.

{% for t in tasks %}
    name: {{t.name}}
    description: {{t.description}}
    priority: **{{t.priority.name}}**
{% endfor %}
S.Lott
ty very much! i know that was something simple :-)))
countnazgul