Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#30217 closed New feature (needsinfo)

Admin form for DateTimeField does should require both fields

Reported by: wKavey Owned by: nobody
Component: contrib.admin Version: 2.1
Severity: Normal Keywords: admin field validation
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: yes

Description

The default Admin form for a DateTimeField seems to be a split form, with separate inputs for both the Date and Time segments. If one provides a Date, but no Time, the form produces a value of 'None' and raises no ValidationErrors.

I would expect at least a ValidationError indicating that one must provide both a Date and a Time. There is already one thrown if no values at all are provided.

I would imaging this could involve setting "require_all_fields=True" on the SplitDateTimeField, however I am not sure.

Somewhat related to https://code.djangoproject.com/ticket/9721

I hit this because I have a custom validation applied in my model's clean() method (datetime_1 <= datetime_2). This is also called in my model's save() method (self.full_clean()). When, using the admin interface, I only specify a Date and no Time, I get an error because one, or both, of my datetimes are None.

Change History (3)

comment:1 by Johannes Maron, 6 years ago

Resolution: needsinfo
Status: newclosed

Hi wKavey,

I just tried to reproduce the issue you described an I can't. If a try to set the User.date_joined via the admin and omit the time, I get This field is required.. I tested this on the latest master.

Maybe share a bit more information about the steps you took and you what you believe the error was.

Best
-Joe

comment:2 by wKavey, 6 years ago

Hmm you're right. I think my issues are stemming from the fact that I force a call to self.full_clean() within my model's save() method

class BasicModel(models.Model):
    dt = models.DateTimeField()

    def clean(self, *args, **kwargs):
        super().clean(*args, **kwargs)

        if self.begin_dt is None:
            raise ValidationError("Cannot be None")

    def save(self, *args, **kwargs):
        self.full_clean()
        return super().save(*args, **kwargs)

I'm unsure of why it would be calling save() before it had completed validation of the form fields... any idea how I could work around this? There are some pretty stringent checks I am attempting to implement before any instance of this model is saved

Version 0, edited 6 years ago by wKavey (next)

comment:3 by Tim Graham, 6 years ago

Please see TicketClosingReasons/UseSupportChannels for ways to get help.

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