How do you make django form validation dynamic?

前端 未结 4 1880
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-13 21:53

I have a form that needs to have either a valid url or a valid file for uploading:

class ResourceUpload(ModelForm):
   ...        
   uploadedfile = forms.Fi         


        
相关标签:
4条回答
  • 2021-01-13 22:42

    my solution
    pros: it keeps the asterisk for the really required field and default error messages

    class Form(forms.ModelForm):
        field1 = SelectField
        field2 = ...
        field3 = ...
    
        def __init__(self, *args, **kwargs):
            super(Form, self).__init__(*args, **kwargs)
            if kwargs['data']:
                if kwargs['data'].get('field1') == '1':
                    self.fields['field2'].required = True
                    self.fields['field3'].required = False
                elif kwargs['data'].get('field1') == '2':
                    self.fields['field2'].required = False
                    self.fields['field3'].required = True
    
    0 讨论(0)
  • 2021-01-13 22:42

    Here's my solution which really works... (tested)

    def __init__(self, *args, **kwargs):
        super(YourForm, self).__init__(*args, **kwargs)
    
        if self.data and self.data.get('field_name') != 'SOMETHING':
            self.fields.get('field_name2').required = True
    

    This makes field_name2 a required field if field_name's input was not 'SOMETHING'. Django rocks!

    0 讨论(0)
  • 2021-01-13 22:47

    You'll need to set them both as required=False, so the database backend doesn't need them both filled in, and then use form cleaning:

    import forms
    
    class ResourceUpload(ModelForm):
       ...        
       uploadedfile = forms.FileField(required = False, upload_to='put/files/here')
       url_address = forms.URLField(required = False) 
       ...
    
       def clean(self):
           cleaned_data = self.cleaned_data
           uploadedfile = cleaned_data.get("uploadedfile ")
           url_address = cleaned_data.get("url_address ")
    
           if not uploadedfile and mot url_address :
               raise forms.ValidationError("Provide a valid file or a valid URL.")
    
           return cleaned_data          
    
    0 讨论(0)
  • 2021-01-13 22:49

    You should take a look at this package django-dynamic-model-validation

    from django.db import models
    from dynamic_validator import ModelFieldRequiredMixin
    
    
    class MyModel(ModelFieldRequiredMixin, models.Model):
        uploadedfile = models.FileField(upload_to='upload/path', blank=True, default='')
        url_address = models.URLField(blank=True, default='') 
    
        REQUIRED_TOGGLE_FIELDS = ['uploadedfile', 'url_address']
    

    This will validate that only one of the fields can be provided raising validation error if both are used.

    0 讨论(0)
提交回复
热议问题