vendredi 19 février 2016

Django ForeignKey in ManytoMany field is displayed as None

I got a model that uses a foreignkey in a manytomany field:

class DefinitionTag(models.Model):
    tag = models.CharField(max_length=50, default="Tag")

    def __str__(self):
        return self.tag


class Definition(models.Model):
    name = models.CharField(max_length=100)
    definition = models.CharField(max_length=1000)
    fundstellen = models.CharField(max_length=300, blank=True)
    wissenswertes = models.CharField(max_length=1000, blank=True)
    tags = models.ManyToManyField(DefinitionTag)

    def __str__(self):
        return self.name

this works, and in the admin everything is set up so i can use it. The problem is if i try to display a table with the database entries in my view with the code:

def home(request):
    query_results = Definition.objects.all()

    context = {
        "query_results": query_results
    }
    return render(request, 'home.html', context)

and in html:

{% for item in query_results %}
    <tr>
        <td>{{ item.name }}</td>
        <td>{{ item.definition }}</td>
        <td>{{ item.fundstellen }}</td>
        <td>{{ item.wissenswertes }}</td>
        <td>{{ item.tags }}</td>
    </tr>
    {% endfor %}

In the tag column it gives me only:

DefinitionTag.None

How can i display there all the tags choosen in the manytomany field?

I hope i'll get a hint! Thanks

Aucun commentaire:

Enregistrer un commentaire