On Mon, 2007-09-17 at 00:23 +0200, Christopher Lenz wrote: > Hey all, > > I've created a ticket which has been closed as wontfix, and was told > to bring it here. So here I am. > > <http://code.djangoproject.com/ticket/5153>
I'm somewhere between -0 and -1 on this, for a couple of reasons. I don't like the design and you can achieve the same functionality without any changes to core. Explanations below... [...] > Now, that use case may sound crazy to you (I don't know why it would, > but hey). But the point is that there is currently NO WAY to do this > with newforms. Honestly, the design of newforms.Form.clean(), where > it deletes its own "cleaned_data" attribute as soon as there's a > single validation error, is totally backwards and unpythonic. There > should be *some* way to get at the cleaned_data values, even if some > of the fields failed to validate. If that was how the code worked, you would have a point. However, Form.full_clean() fills cleaned_data with the normalised forms of any fields that validated and puts any errors into the internal error list. So fields that are correct *do* appear in cleaned_data and it is not erased when an error is encountered. Reading your patch, it looks like you are actually going through form_for_model or form_for_instance, since your real data flow changes are in newforms/models.py. Since saving data into models absolutely requires valid data, being able to disable data validation there would be illogical. I also don't like the change to disable validation in Form.full_clean(), because that makes it kind of a no-op: cleaning is all about validation and normalisation. Normalisation has to happen after or in parallel with validation, otherwise the normalisation code would have to be prepared to handle every possible input format under the sun, instead of just the valid types. So once you disable validation, normalisation also doesn't occur and there's nothing for cleaning to do. We'd be adding an extra code path in a few places that has be maintained and kept correct in the future and it's only purpose is to subvert the whole cleaning process. Feels less neat than it should be to me. However, here are two alternative approaches, neither of which require changing any core code. Firstly, the "whole form at once" approach, based on subclassing: you already have a form with all the field definitions in it. Since the main problem you seem to be working around is that not all data is necessary, create a subclass of that form where the __init__ method alters the fields and sets all the "required" statuses to False. So every field becomes optional. Maybe you also want to override clean() in your subclass to pull all data that may have been marked as invalid because it was present but in the wrong format into cleaned_data and clean out _errors. Then you give your partial data to this subclass, and it can be validated as normal. Once you have *all* the data for the form, you can reuse the original class for validation and it will complain about missing fields as well. If you have further, looser validation requirements for the intermediate stages of entry, you can add methods to your subclass as needed. You could also adopt something like this with your "fake model" approach, by ensuring the dummy model you are setting up has all fields being optional. The second approach, that would seem more suitable to Ajax work, is to only be validating the single fields that you are submitting. I'd do that by calling the clean methods on those fields (plus any clean_<fieldname> methods) directly, rather than re-validating the whole form. Whether this is an easier approach depends on your particular forms, obviously. In both of these cases, nothing needs to be added to Django. Further, I would personally be a bit reluctant at this point to even add utility methods for this case, since it's not the only possible variation on standard data entry that people might want and it's just not that much code to write in the cases it's needed. So I'm not convinced that we need to accommodate all that flexibility as helper functions when it's already just a subclass away (and writing a routine to generate the subclass for one's own particular circumstances isn't too onerous). Regards, Malcolm -- Experience is something you don't get until just after you need it. http://www.pointy-stick.com/blog/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django developers" 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-developers?hl=en -~----------~----~----~----~------~----~------~--~---
