django-commons / django-commons/django-polymorphic

Polymorphic Filtering (for fields in inherited classes) enhance

Open
#233 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
1.8k
Forks
304
Avg merge
5h 23m
Merged PRs (30d)
4

Description

Sorry in advance I wanted to label this an enhancement but the labels aren't showing up.

`class Project(PolymorphicModel):
topic = models.CharField(max_length=30)

class ArtProject(Project):
artist = models.CharField(max_length=30)

class ResearchProject(Project):
supervisor = models.CharField(max_length=30)

class ScienceProject(Project):
supervisor = models.CharField(max_length=30)
other_field = models.BooleanField()`

I may have missed this in the documentation but if two inherited models share a common field in this case the supervisor field shared by the Research and Science project is there a cleaner way to make a query on supervisor other than.

results = Project.objects.filter(Q(ResearchProject___supervisor="Bob") | Q(ScienceProject___supervisor="Bob"))

something like

results = Project.objects.instance_of(ResearchProject, ScienceProject).filter(supervisor="Bob")

or even better would be

results = Project.objects.filter(supervisor="Bob")

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the Project, ResearchProject, and ScienceProject model examples and compare the existing Q-based query with the proposed Project.objects.instance_of(...).filter(supervisor="Bob") forms. Investigate the polymorphic manager and Django ORM filtering behavior. Done means a shared inherited field can be filtered without manually OR-ing subtype-specific paths, with behavior covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
backend, database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.