Answer the question
In order to leave comments, you need to log in
How to get rid of duplicate information in django?
there is a model like this:
class Series(models.Model):
title = models.CharField(max_length=400)
description = models.TextField(blank=True)
class Book(models.Model):
title = models.CharField(max_length=400)
description = models.TextField(blank=True)
series = models.ManyToManyField(Series, through='BookSeries', blank=True)
summary = models.ForeignKey(BookSummary)
class BookSummary(models.Model):
original_title = models.CharField(max_length=400)
description = models.TextField(blank=True)
class BookSeries(models.Model):
book = models.ForeignKey(Book)
series = models.ForeignKey(Series)
booksummary = models.ForeignKey(BookSummary)
position = models.IntegerField()
def save(self, *args, **kwargs):
self.booksummary = self.book.summary
super(BookSeries, self).save(*args, **kwargs)
class BooksSummary(DetailView):
model = BookSummary
def get_context_data(self, **kwargs):
context = super(BooksSummary, self).get_context_data(**kwargs)
context['books'] = self.object.book_set.all()
context['series'] = self.object.bookseries_set.all() # проблема тут
return context
context['series'] = Series.objects.filter(book__summary=self.object).distinct()
Answer the question
In order to leave comments, you need to log in
I solved the problem, albeit in a way that I didn't like very much. I don't feel satisfied with it ;)
up to this point, the dev version was in SQLite, for the sake of convenience. I had to switch to PostgreSQL for the improved .distinct().
now the view looks like this:
class BooksSummary(DetailView):
model = BookSummary
def get_context_data(self, **kwargs):
context = super(BooksSummary, self).get_context_data(**kwargs)
context['books'] = self.object.book_set.all()
context['series'] = self.object.bookseries_set.all()\
.select_related('series',
'book',
'booksummary')\
.order_by('series')\
.distinct('series')
return context
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question