0

Model:

class Specialization(models.Model):
    SPECIALIZATION_TYPE = (
    ('S','Specialty'),
    ('Ss','Sub-specialty')
    )
    specialization_desc = models.CharField('Specialization',max_length=50,unique=True)
    specialization_type = models.CharField(max_length=2, choices=SPECIALIZATION_TYPE)
    active = models.BooleanField('Active',default=True)

    def __unicode__(self):
    return self.specialization_desc

class Person(models.Model):
    GENDER = (
    ('M','Male'),
    ('F','Female'),
    )
    first_name = models.CharField("First Name", max_length=50)
    last_name = models.CharField("Last Name",max_length=50)
    middle_name = models.CharField("Middle Name", max_length=50, blank=True)
    specialization_id = models.ManyToManyField(Specialization, 

Template:

{% for per in person_list%}
<tr>
<td>{{ per }}</td>
{% for spec in per.specialization_id.all %}
    <td>{{ spec }}</td>
{% endfor %}
</tr>
{% endfor %}

View:

p = Person.objects.all()
return p

I would Like to see a table like this:

FullName | Specialization               |
My Name  | Programming, Web Development |

I'm getting this instead

FullName | Specialization |
My Name  | Programming    | Web Development
  • Storing spec in a variable is not possible accdg to articles that I've read, that alone should have solved my problem
  • Storing data in dictionary from views is giving me object Person is not callable

Seems a dead end for me. Ideas?

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
splucena
  • 159
  • 1
  • 3
  • 14
  • 1
    I've no idea why you think "storing spec in a variable" is not possible, or indeed why you think it would solve your problem. And if "storing data in dictionary" is giving you an error you should show that code and the actual error, rather than a vague description. – Daniel Roseman Jan 21 '14 at 08:58
  • It just conveys my limited knowledge of Django but I'm working hard on it. What I meant was creating a variable to store data in template. – splucena Jan 22 '14 at 00:03

1 Answers1

2

I don't get your question.

Do you get the specializations in template and only need to display them differently?

{% for person in person_list%}
    <tr>
        <td>{{ person }}</td>
        <td>{{ person.specialization_id.all|join:", " }}</td>
    </tr>
{% endfor %}

Furthermore, don't suffix foreign keys and many-to-many relations with _id.

For foreign keys Django does it for you already, so in DB you end up with field_id_id.

For many-to-many a separate table is created and the specialization_id isn't created anywhere in the DB. Use more verbose name, like specializations instead.

Krzysztof Szularz
  • 5,151
  • 24
  • 35
  • I'll put those feedback into practice. You saved my day, now I can moved on! Thanks so much! – splucena Jan 22 '14 at 00:07
  • {% for d in per.specialization_id.all %} {{ d.specialization_type }} {% endfor %} -- Is there a shorter way to get the specialization_type so I don't have to use forloop? – splucena Jan 22 '14 at 04:32
  • Yes. `per.specialization_id.all.values_list('specialization_type', flat=True)`. It makes one SQL query. You can `|join:', '` it as well. – Krzysztof Szularz Jan 22 '14 at 08:53
  • per.specialization_id.all.values_list('specialization_type', flat=True) -- I tried to use this in the template, it gave me an error cannot parse remainder:('specialization_type',flat=True). I know I can use this in the view but I'm not really sure how to do it. Currently in my view it's just p=Person.objects.all – splucena Jan 23 '14 at 00:37
  • DB queries are not something you want to keep in views. For-loop is sufficient in your case. If there are too many queries, use `select_related` and `prefetch_related` in the view. Please refer to django docs for more info about those methods. – Krzysztof Szularz Jan 23 '14 at 08:27