Removing duplicates due to order by fields from a related model

Removing duplicates due to order by fields from a related model

I have 2 different models in the same app

I have 2 different models in the same app

class Book(models.Model):
    title = models.CharField(max_length=250, unique=True)
    slug = models.SlugField(max_length=50)
    description = models.TextField(max_length=500)

class Chapter(models.Model): book = models.ForeignKey(Book,on_delete=models.CASCADE) num = models.PositiveIntegerField(verbose_name="Chapter Number") rdate = models.DateTimeField(verbose_name="Release Date", default=timezone.now)

What I want to do is get all books ordered by descending of release date rdate What I tried is:

Book.objects.all().order_by('-chapter__rdate').distinct()

Some duplicates were found in result, I only had 2 books added, so only 1 book was duplicated. The result was something like this: Book1, Book2, Book2

I have already seen the note from here, and im using SQLite. By using values(), I won't be able to call get_absolute_url() and some other methods that I want to call so using values() is out of question.

Next I tried to annotate the Chapter rdate into Book query like thisBook.objects.all().annotate(bdate=ExpressionWrapper(F('chapter__rdate'), output_field=DateTimeField())).order_by('-bdate').distinct()

Still ended in the same result. I'm totally lost, please need help. I probably won't change to PostgreSQL now, but I might use it when I deploy the app, so I want both SQLite version and PostgreSQL version solutions for this if at all possible. SQLite solution is absolutely necessary for me since it's easy to use in development.

django

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Python Django Tutorial | Django Course

🔥Intellipaat Django course: https://intellipaat.com/python-django-training/ 👉This Python Django tutorial will help you learn what is django web development &...

django-allauth Django REST Framework |teratail

Djangoアプリを、当初は Django REST Framework (DRF) を使わずに作成しました。(認証パッケージは、 django-allauthを使用)アプリリリース後に、DRFも追加で導入して、DRFで作成したAPIでのデ

Django REST Framework Course for Beginners

In this article we are going to talk about Django REST Framework Course for Beginners, also for more information you can check the complete video for th

Хранение изображений с помощью Django/Django REST

Проблема Мне в ходе разработки часто приходится работать с моделями, в которых должны быть изображения. Для удобной организации я использую древовидную структуру...

How to Integrate Django to any Javascript Front End // Django to React // Django to Angular

Learn the basic technique to Integrate Django to any Javascript Front End // Django to React // Django to Angular