django-validation

Django: Overriding the clean() method in forms - question about raising errors

China☆狼群 提交于 2020-01-01 03:10:23
问题 I've been doing things like this in the clean method: if self.cleaned_data['type'].organized_by != self.cleaned_data['organized_by']: raise forms.ValidationError('The type and organization do not match.') if self.cleaned_data['start'] > self.cleaned_data['end']: raise forms.ValidationError('The start date cannot be later than the end date.') But then that means that the form can only raise one of these errors at a time. Is there a way for the form to raise both of these errors? EDIT #1 : Any

Validate new users passwords in Django admin interface

China☆狼群 提交于 2019-12-25 03:13:11
问题 There is one interesting moment in Django documentation: By default, validators are used in the forms to reset or change passwords and in the createsuperuser and changepassword management commands. Validators aren’t applied at the model level, for example in User.objects.create_user() and create_superuser(), because we assume that developers, not users, interact with Django at that level and also because model validation doesn’t automatically run as part of creating models. But in my Django

Django Custom Model Field with Validation…how to hook it back to ModelForm

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 18:35:59
问题 A common occurrence I have with one particular project is that it requires the user to enter dimensions (for width/depth/height) in Feet and Inches. Calculations are needed to be performed on that dimension, so I've been working on a custom field type that takes in a dimension in Feet/Inches (eg. 1'-10") and saves it to the database as a decimal number using a regex to parse the input. The field displays to the end-user as feet-inches at all times (with the eventual goal of writing a method

Django TextField max_length validation for ModelForm

风格不统一 提交于 2019-12-20 09:29:20
问题 Django does not respect the max_length attribute of TextField model field while validating a ModelForm. So I define a LimitedTextField inherited from the models.TextField and added validation bits similar to models.CharField: from django.core import validators class LimitedTextField(models.TextField): def __init__(self, *args, **kwargs): super(LimitedTextField, self).__init__(*args, **kwargs) self.max_length = kwargs.get('max_length') if self.max_length: self.validators.append(validators

Django / DRF - How can I view a list of all validators for a model / model serializer field?

百般思念 提交于 2019-12-13 18:35:04
问题 This is my UserExtendedSerializer: class UserExtendedSerializer(serializers.ModelSerializer): def __init__(self, *args, **kwargs): super(UserExtendedSerializer, self).__init__(*args, **kwargs) # call the super() for field in self.fields: # iterate over the serializer fields self.fields[field].error_messages['required'] = 'Enter a valid %s.'%field # set the custom error message self.fields[field].error_messages['invalid'] = 'Select a valid %s.'%field # set the custom error message class Meta:

Cleanest Way to Allow Empty Scheme in Django URLField

半世苍凉 提交于 2019-12-12 10:57:02
问题 I'm using Django 2.0 and trying to allow users to input a link to their website in a django ModelForm. I would like for my URLField to validate urls even when they have no scheme prefix. I have read this solution: django urlfield http prefix. I'm wondering if there is a way to achieve this without writing my own custom regex in a RegexValidator. It doesn't seem like a very DRY solution to repeat all of the regex logic that is built into the URLValidator. I want to do something like the below:

Custom widget with custom value in Django admin

China☆狼群 提交于 2019-12-11 19:28:31
问题 In model I have integer field. Meaning of value of this field is "number of days since 1.1.1970", so I wanted to display it in admin interface as a date using AdminDateWidget to allow easier selection of date. My only idea is to create new widget to translate integer to date: class ExpireWidget(AdminDateWidget): def render(self, name, value, attrs=None): if isinstance(value, (int, long)): value = datetime.date(1970, 1, 1) + datetime.timedelta(value) return super(ExpireWidget, self).render

'str' object has no attribute 'label' when validating

社会主义新天地 提交于 2019-12-11 19:25:32
问题 Okay, this is a weird one, at least to me. I have a form that contains event_date and article_date . I want to confirm that event_date is not bigger than article_date , because that would mean that the event would take place in the future, which is not possible. Now, what I did is this: def clean(self): cleaned_data = self.cleaned_data event_date = cleaned_data.get("event_date") location = cleaned_data.get("location") article_date = cleaned_data.get("article_date") if event_date and location:

Django model field validator function used for update or insert [closed]

纵饮孤独 提交于 2019-12-11 19:13:42
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . Is there a way of figuring out inside a Django model field validator function if it used for update or insert? 回答1: No, there isn't. A validator is simply a callable that takes a value and checks it. It has no

Why Model field validation happens before validate or validate_<field> is called?

╄→尐↘猪︶ㄣ 提交于 2019-12-11 06:23:51
问题 I just started learning Django Rest Framework and I can't understand why DRF runs Model field validation before Own Validation I have a model which has a URLField and basically all I want to add http:// or https:// before it validates, so wrote custom validation method class ShortenerSerializer(serializers.ModelSerializer): class Meta: extra_kwargs = { 'count': {'read_only':True} } fields = ('id', 'url', 'short_url', 'count', 'created_at') model = Shortener def validate_url(self, url): if not