Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top