I have two classes: Project which has a many-to-one relationship with Client. I would like my detailed template of Client to display a list of projects that are related to it.
The code below successfully gets the required Projects, and stores them in context, which is then passed to the template renderer which renders them successfully.
So far, so good. But the rendered table will not "sort" the data successfully. I can click on the headers, which will add ?sort=name to the request, but nothing happens.
Is this the correct approach for what I am intending, and if so, what step am I missing to get the full functionality?
models.py
class Client(models.Model):
name = models.CharField("Name", max_length=300, unique=True)
class Project(models.Model):
client = models.ForeignKey("Client", on_delete=models.RESTRICT)
name = models.CharField("Name", max_length=300, unique=True)
tables.py
import django_tables2 as tables
from .models import *
class ProjectTable(tables.Table):
class Meta:
model = Project
template_name = "django_tables2/bootstrap.html"
fields = ("name", )
views.py
class ClientDetailView(DetailView):
model = Client
def get_context_data(self, **kwargs):
# Get related Projects and put then in 'table'
table = ProjectTable(self.object.project_set.all())
# Add 'table' to 'context'
context = super(ClientDetailView, self).get_context_data(**kwargs)
context['table'] = table
return context
html
{% load render_table from django_tables2 %}
{% render_table table %}