0

i have written validaiton function for an attribute is it correct and how should i write for same attribute with blank = True and add_1 is required field any conditions to add

add_1 = models.CharField(max_length=255)
add_2 = models.CharField(max_length=255, blank=True)

Note: All validators must return True or False

validators.py

def validate_add_1(value):
    if value is not None:
        try:
            if len(value) <= 255:
                return True
        except ValidationError:
            return False
Itriedit
  • 71
  • 1
  • 7

2 Answers2

1

According to Model Reference, when you add the "blank=True" attribute to any model field, that that field becomes optional. If you want a field to be required, do not specify blank attribute as the default is blank=False.

For validator, I'm not sure what you are trying to do, but you can try something like this:

def validate_add_1(value):
    val_len = False if len(value) > 255 else True
    # Return True if value is not blank/null and length of value <= 255
    return True if val_len and (value and value != '') else False

Edit

To simplify the above code for you to understand:

def validate_add_1(value):

    # If length of value > 255, return False, else check for blank = True

    if len(value) > 255:
        return False
    else:

        # If value is not blank, AND value is not an empty string.
        # This is for checking blank = True condition is satisfied.

        if value and value != '':
            return True
        else:
            return False
Naeem Khan
  • 950
  • 4
  • 13
  • 34
  • Thanks @Naeem Khan how do i write for same with blank = True – Itriedit Jan 30 '22 at 05:20
  • This already checks for blank = True. The (value and value != '') condition checks if the value is not null (i.e. not blank) or if it has and empty string which is sometimes the case if you pass an empty input field from form. – Naeem Khan Jan 30 '22 at 07:05
  • You can check my edited answer for a simplified version. Why do you need to do these checks in the first place? – Naeem Khan Jan 30 '22 at 07:16
0
def validate_add_2(value):
    val_len = False if len(value) > 255 else True
    return True if value == '' or (value and val_len) else False

does this work?

Itriedit
  • 71
  • 1
  • 7
  • You can remove this answer and post this snippet in your original post. This will not work as it will only check for either condition A (value == '') OR condition B. So if condition A returns True, even if the second condition is False, you will still get True. – Naeem Khan Jan 30 '22 at 07:08
  • @NaeemKhan I will remove this but this is for add_2 field which is not a required field if value == "" then it can return True also if value is given it should satisfy max_length doesn't this work for it? – Itriedit Jan 30 '22 at 07:18
  • 1
    Then all you need in your function is this line: `return False if len(value) > 255 else True` – Naeem Khan Jan 30 '22 at 07:20
  • @NaeemKhan This validators are for checking the spreadsheet data not adding as validators in model field i have a website which has headers of the spreadsheet as fields and i have to map the data of the spreadsheet with django models when spreadsheet is uploaded it will validate the headers first and then row data i need to add this validation function to validate the spreadsheet data before mapping it with model fields – Itriedit Jan 30 '22 at 07:26