I have a model with two DateTimeFields, named start and end. As Python
datetime objects, I'd like to ensure that (start < end) for all valid
instances of this model. What's the right way to enforce this
invariant?
I tried making a custom Validator object and adding it to
start.validator_list, and then trying to enter an invalid pair of
datetimes in the admin interface, but the problem seems to be that the
actual representation in the all_data parameter is divided between
"start_date" and "start_time" (and "end_date" and "end_time"). These
are simply the form element names, and the all_data dictionary contains
the raw strings inputted from the form.
Two reasons why I don't like this:
1. I really don't think that this is the right place to be putting
this validator, as I want the normal DateTimeField validators to
run too, and I don't want to have to duplicate their work. (I
even appended mine to the end of validator_list, but it seems to
get caught anyway.)
2. I also don't want to have to be cleaning up the user's mess with
strings and just want to be operating on two (Python) datetime
objects.
My "big stick" solution is just to override the save() method of the
model, and to have it swap the start and end fields. I'm sure this
approach can be made to work (neglecting the start==end case), but
wanted to see in general if there was a more general way to enforce
model invariants, particularly if it's something where real user
interaction is needed to correct it.
Best,
Mike
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---