Opened 4 months ago

Closed 4 months ago

#33039 closed New feature (invalid)

Django to have conditional column based on another column

Reported by: Nathaniel Brown Owned by: nobody
Component: Database layer (models, ORM) Version: 3.2
Severity: Normal Keywords: conditional, condition, alternate columns
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I am looking to do a pre-fetch on a data model that conditionally loads either of two columns based on what's in another column.

Simply put, if the title tag has <file> in it, then the model should load the file_value column. If it does not, then load the body column.

I'd like to use it with the get object or 404 if possible.

poem = get_object_or_404(Poem, pk=poem_id)

Change History (1)

comment:1 Changed 4 months ago by Mariusz Felisiak

Resolution: invalid
Status: newclosed

You can add an annotation based on Case() expression to the base queryset.

Please use one of support channels if you have further questions.

Note: See TracTickets for help on using tickets.
Back to Top