I am new to Django and I want to query a list inside of an object. I have a list of Batches and each batch has a BatchComment list. Both of them has a User property. What I want to do is get batches where user has a comment and only get ones where the last comment is not made by the user. How can I achieve this?
Currently I am retrieving batches that user has comment by Batch.objects.filter(comments__user=self.request.user)
I want something like Batch.objects.filter(comments__user=self.request.user).filter(comments_last__user!=self.request.user)
Here are my models:
class Batch(TimeStampMixin):
note = models.TextField(max_length=1000)
image_url = models.URLField()
class Meta:
db_table = 'batches'
ordering = ['-created_at']
class BatchLogComment(TimeStampMixin):
body = models.TextField(max_length=500)
batch = models.ForeignKey(Batch, on_delete=models.CASCADE, related_name='comments')
user = models.ForeignKey(
User, on_delete=models.SET_NULL, blank=True, null=True, related_name='comments')
class Meta:
db_table = 'batch_log_comments'
ordering = ['-created_at']