Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate rows when ordering by related_name #957

Open
tegraandrew opened this issue Aug 12, 2024 · 0 comments
Open

Duplicate rows when ordering by related_name #957

tegraandrew opened this issue Aug 12, 2024 · 0 comments

Comments

@tegraandrew
Copy link

tegraandrew commented Aug 12, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant