Duplicate rows when ordering by related_name
tegraandrew opened this issue · comments
I'm having an issue where the table renders correctly when viewed by default, but when I sort on the related_name, last_response, it duplicates rows multiplied by the number of related objects. I've simplified my code down to just the problem area.
My code:
tables.py
class InquiryTable(tables.Table):
last_response = tables.columns.DateTimeColumn(accessor="messages", verbose_name="Last Response Sent", )
def render_last_response(self, value,):
return value.order_by("-date").first().date.strftime("%Y-%m-%d %H:%M:%S")
class Meta:
model = Inquiry
attrs = {"id": "inquiryTable", 'class': 'table table-light table-striped',}
fields = ("last_response",)
models.py
class Inquiry(models.Model):
status = models.CharField(max_length=20, default="OPEN")
title = models.CharField(max_length=255)
class Message(models.Model):
inquiry = models.ForeignKey(Inquiry, on_delete=models.CASCADE, related_name="messages")
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, verbose_name="Sender", related_name="users")
date = models.DateTimeField(auto_now_add=True)
message = models.CharField(max_length=1000)
Should I just try reconstructing the table so that model = Message instead of model = Inquiry and just filter it down and use a bunch of accessors?