Opened 14 years ago

Closed 14 years ago

#14347 closed (wontfix)

Add required_if field to model fields

Reported by: hejsan Owned by: nobody
Component: Database layer (models, ORM) Version:
Severity: Keywords:
Cc: hr.bjarni+django@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I propose to add a required_if field to model fields (and form fields also i guess?)
It should take a callable that returns either True or False.

Another option would be to allow the required field to accept a callable.

Uses:
Very often we have a "Published" field on our models (or "Published date" or "Published status" etc..) It would be very nice to be able to allow people to save instances without all the required fields being filled in IF the article or whathaveyou is not published yet.

Currently to do this you have to make all the fields not required and then override the save method. This would be a very handy feature for a very common problem.

Caveat: This means that the field should not be declared as "NOT NULL" in the database, but I don't really see that as a problem if validation is performed by django anyway.

Change History (4)

comment:1 by hejsan, 14 years ago

Update: I wrote to impulsively, this should have nothing to do with the model field, as the model field only specifies null and blank, this should be a property of the form field.

The title of this ticket should be "Add required_if field to form fields". Sorry about that.

comment:2 by David Novakovic, 14 years ago

FWIW this sounds like it's related to this discussion too http://groups.google.com/group/django-developers/browse_thread/thread/a040530cf2614824#

Basically dealing with validation over multiple fields. I know yours is a param to the field and Yo Yo was talking about something like Meta, but yeah, they are conceptually related :P

Seems like the jury was out on this..

comment:3 by Daniel F Moisset, 14 years ago

Component: UncategorizedDatabase layer (models, ORM)

comment:4 by Russell Keith-Magee, 14 years ago

Resolution: wontfix
Status: newclosed

This is possible to implement using model (or form) validation. I don't see any reason to provide a shortcut syntax for one very specific use case of validation.

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