views:

53

answers:

1

I have 03 models book->target_readers and book has collection.

class Book(models.Model):
    def __unicode__(self):
        return "%s (%s)" % (self.title, self.reference)
    reference = models.CharField(max_length=100)
    title = models.CharField(max_length=200)
    collection = models.ForeignKey(Collection)

class Collection(models.Model):
    def __unicode__(self):
        return self.name
                                       )
    name = models.CharField(max_length=200)
    target_readers = models.ManyToManyField(Target_Readers, blank=True, null=True)

class Target_Readers(models.Model):
    def __unicode__(self):
        return self.name
    name = models.CharField(max_length=200, verbose_name="target reader")

In my template i do like this to get the target_readers of the book

{% for book in books %}
   {{ book.title }}
   {{ book.public_price }}
   {{ book.collection.target_readers.name }}

But I don't get the any results for target reader.

Anyone know how can I do this?

+3  A: 

Try:

{% for book in books %}
   {{ book.title }}
   {{ book.public_price }}
   {% for reader in book.collection.target_readers %}
       {{ reader.name }}
   {% endfor %}
{% endfor %}

target_readers is not a single entity, it's a list (albeit one which may just contain a single element).

Dominic Rodger
not working too.
python
@python - in what way?
Dominic Rodger