I have the following models:
class Work_Music(MPTTModel, Work):
name = models.CharField(max_length=10, null=True, blank=True)
class Cast(models.Model):
name = models.CharField(max_length=100, null=True, blank=True)
def __str__(self):
return self.name
class WorkCast(models.Model):
work = models.ForeignKey(Work_Music, verbose_name=_('work'), related_name='workcast', null=True, blank=True, on_delete=models.PROTECT)
cast = models.ManyToManyField(Cast, verbose_name=_('cast'), related_name='workcast', blank=True)
def __str__(self):
return "%s" % (
", ".join(character.name for character in self.cast.all())
)
This will output: Character #1, Character #2, Character #3.
I pass in my view.py this QuerySet as context:
work = Work_Music.objects.get(pk=self.kwargs['pk']).get_descendants(include_self=False)
How do display the string that is returned when you call an instance of the WorkCast model (e.g. "Character #1, Character #2, Character #3")?
I have this currently:
{{ work.workcast }}
and it displays None
Since you query a ForeignKey in reverse, the number of related WorkCast models is a collection that can contain zero, one, or more WorkCast objects. You thus should iterate over it, like:
{% for workcast in work.workcast.all %}
{{ workcast }}
{% endfor %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With