NoReverseMatch on template view with through model

12 views
Skip to first unread message

toneldaclan

unread,
Apr 8, 2018, 8:34:55 PM4/8/18
to Django users

This code in my views.py works fine when it was still not in template format

def detail(request, entity_group_id):
    entity_group = get_object_or_404(EntityGroup, pk=entity_group_id) # noqa
    entity_list = entity_group.members.order_by('name')
    context = {
        'entity_group': entity_group,
        'entity_list': entity_list,
    }
    return render(request, 'core/detail.html', context)

When I changed it into a Generic View a NoReverseMatch comes up...

class DetailView(generic.DetailView):
    model = EntityGroup
    template_name = 'core/detail.html'

    def get_context_data(self, **kwargs):
        context = super(DetailView, self).get_context_data(**kwargs)
        context['entity_group'] = EntityGroup
        context['entity_list'] = EntityGroup.members
        return context

Here is my models.py

class Entity(models.Model):
    name = models.CharField(max_length=30)


class EntityGroup(models.Model):
    name = models.CharField(max_length=20)
    members = models.ManyToManyField(Entity, through='Membership')

class Membership(models.Model):
    entity_group = models.ForeignKey(EntityGroup, on_delete=models.PROTECT, null=False)
    entity = models.ForeignKey(Entity, on_delete=models.PROTECT, null=False)

How do I set a reverse on the Generic view?

Reply all
Reply to author
Forward
0 new messages