python - Check if a form is valid from within a form field [Django] -


I have sub-squared the text-field form field in Django to create my own custom widget for a field I was wondering if it is possible to check that all other fields of the form are valid (I want to separate my server side behavior based on the recognition of other areas)

See the comment

Something like this:

  class CustomField (TextInput): def __init __ (self, * args, ** kwargs): ... soup (CustomField, itself) .__ init __ (* arg, ** kwargs) Input_type = 'hidden' def value_from_datadict (auto, data, file, name): aws_file_key = data.get (name, none) _media_bucket = boto.connect_s3 (Settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY) .lookup (settings.AWS_MEDIA_STORAGE_BUCKET_NAME) Try: except key: _media_bucket.get_key (aws_file_key): Failed to get print 'key.' Key = none if key and aws_file_key: fh = tempfile.TemporaryFile () key.get_contents_to_file (fh) fh.seek (0) files = plainloaded file (key.name, fh.read ()) ### If the form valid delete key Otherwise, keep it if code_to_check_if_valid: _media_bucket.delete_key (key) fh.close () returns file ...... etc    

If you want to validate a certain field based on the values ​​of other fields, then you must put it at the form level and the clean method of the field To overrun You have to burn - they are very good. CustomForm (custom.form): custom_field = CustomField () DEF clean (self): cleaned_data = super (CustomForm, auto) .clean () custom_field = cleaned_data.get ("custom_field" ) ...

, you will see that if all other areas are independently verified, at this step, clear method It can be considered valid:

These methods run one field at a time in the order given above. That is, as each field (they are declared in form definition), Field.clean () method (or its override) is running, then clean_ . Finally, once those two methods are run for each field, the Form.clean () method or its override is executed.

The last clean method is by default if you have any errors, so you do not have any errors, to ensure that you get through cleaned_data It will have to iterate

The clean () method or subclass for the form class is always running. If that method raises a validation error, then the cleaned_data will be an empty dictionary.

The last paragraph means that if you have the form. If you are overriding the scales, then you should iterate through self.cleaned_data.items (), possibly even on the Rounds Dictionary Attribution On Form. In this way, you may already know which areas have exceeded their individual verification requirements.

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -