This code works well:

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 converted it to a Generic View, a Reverse not found error occured

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

This is mu models.py

class Entity(models.Model):
    is_person = models.BooleanField(default=True)
    person = models.ForeignKey(Person, on_delete=models.PROTECT, null=True) 
# noqa
    company = models.ForeignKey(Company, on_delete=models.PROTECT, 
null=True) # noqa
    name = models.CharField(max_length=30)

    def __str__(self):
        return self.name

    @property
    def title(self):
        return self.name

    class Meta:
        verbose_name_plural = 'entities'


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

    def __str__(self):
        return self.name

    @property
    def title(self):
        return self.name


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

    def __str__(self):
        return self.entity_group.name + '-' + self.entity.name

    @property
    def title(self):
        return self.entity_group.name + '-' + self.entity.name

    class Meta:
        unique_together = ("entity", "entity_group")

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/90df6201-c304-40d4-96db-90b944838513%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to