Django 4.2: Difficulty with Conditional Filtering on ModelForm for Inline Related Models
I'm refactoring my project and I'm refactoring my project and I keep running into I'm collaborating on a project where I've been struggling with this for a few days now and could really use some help. I'm working on a personal project and I'm trying to implement conditional filtering on a `ModelForm` for inline related models in Django 4.2. I have a main model `Book` and a related inline model `Review`. I want to conditionally filter `Review` instances based on the `Book` being edited in the admin panel. Specifically, I want to show only the reviews that have a rating above 3 when I'm editing a book. Here's the relevant part of my `admin.py` file: ```python from django.contrib import admin from .models import Book, Review class ReviewInline(admin.TabularInline): model = Review extra = 1 def get_queryset(self, request): qs = super().get_queryset(request) if self.parent_obj: return qs.filter(book=self.parent_obj, rating__gt=3) return qs class BookAdmin(admin.ModelAdmin): inlines = [ReviewInline] def get_inline_instances(self, request, obj=None): self.parent_obj = obj # Pass the current Book instance to ReviewInline return super().get_inline_instances(request, obj) admin.site.register(Book, BookAdmin) ``` When I run this, I'm getting the following behavior: ``` AttributeError: 'NoneType' object has no attribute 'id' ``` It seems like `self.parent_obj` is `None` when `get_queryset` is called, leading to this behavior. I've tried checking if `obj` is not `None` in various places, but I'm not able to grasp how `get_queryset` is being executed in relation to the object instance. I would appreciate any insights or best practices on how to achieve this filtering correctly without running into this scenario. This is part of a larger web app I'm building. What's the best practice here? For context: I'm using Python on macOS. Has anyone else encountered this? The stack includes Python and several other technologies. What's the correct way to implement this? I'm working with Python in a Docker container on CentOS. Hoping someone can shed some light on this. Thanks in advance! The project is a application built with Python. Is this even possible?