django-validation

How can I create sophisticated Django Model Validation for Django Admin?

佐手、 提交于 2019-12-10 17:09:11
问题 I have the following model in Django: class Bout (models.Model): fighter_1 = models.ForeignKey(Fighter, related_name="bout_fighter_1") fighter_2 = models.ForeignKey(Fighter, related_name="bout_fighter_2") winner = models.ForeignKey(Fighter, related_name="bout_winner", blank=True, null=True, help_text='Leave blank for draw.') date = models.DateField() cancelled = models.BooleanField() I would like to "idiot-proof" the administration for its records. Incidently, I want to create three rules:

Django field validation in Model and in Admin?

你。 提交于 2019-12-09 15:30:40
问题 I want to define my own validation routine for a particular field of a Django model. I want the error message to be displayed in the admin form but I also want the same validation to take place if the entity is saved by own python code. Is there a way to do this without breaking the DRY principle? 回答1: If you want to validate an individual field, you can write a validator and add it to your model field. The validator will be run for the field whenever the model's full_clean method is called.

django form validation based on whether field has value

浪尽此生 提交于 2019-12-08 12:18:30
问题 When I submit an empty form with a phone_type selected (for formHomePhone ) the form returns its self without a value selected in phone_type stipulating This field is required As you can see from the view The first phone number in the form is required but the other phone numbers are not. I only want to process them if there is a value present. Though when I click submit on an empty form the additional phone number fields throw up an error from the UKPhoneNumberField > Phone number must

Django CreateView: set user before validation

风流意气都作罢 提交于 2019-12-06 09:52:45
I have a model that uses different validation for its name field depending on whether the object was created by a user or by the system. class Symbol(models.Model): name = models.CharField(_('name'), unique=True, max_length=64) creator = models.ForeignKey('User', null=True, on_delete=models.CASCADE) def is_system_internal(self): """ whether or not this Symbol belongs to the system rather than having been created by a user """ return (self.creator is None) def clean(self): """ ensure that the Symbol's name is valid """ if self.is_system_internal(): if not re.match("^_[a-zA-Z0-9\-_]+$", self

Django field validation in Model and in Admin?

自闭症网瘾萝莉.ら 提交于 2019-12-04 03:09:52
I want to define my own validation routine for a particular field of a Django model. I want the error message to be displayed in the admin form but I also want the same validation to take place if the entity is saved by own python code. Is there a way to do this without breaking the DRY principle? If you want to validate an individual field, you can write a validator and add it to your model field. The validator will be run for the field whenever the model's full_clean method is called. It will be run whenever a model form is validated (including in the Django admin), but it will not

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

时光怂恿深爱的人放手 提交于 2019-12-03 06:39:58
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 solutions for the above are great, but would love something that would also work in a scenario like: if

Django TextField max_length validation for ModelForm

戏子无情 提交于 2019-12-02 18:56:42
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.MaxLengthValidator(self.max_length)) def formfield(self, **kwargs): defaults = {'max_length': self.max

Django unique_together with nullable ForeignKey

蓝咒 提交于 2019-11-30 07:55:10
问题 I'm using Django 1.8.4 in my dev machine using Sqlite and I have these models: class ModelA(Model): field_a = CharField(verbose_name='a', max_length=20) field_b = CharField(verbose_name='b', max_length=20) class Meta: unique_together = ('field_a', 'field_b',) class ModelB(Model): field_c = CharField(verbose_name='c', max_length=20) field_d = ForeignKey(ModelA, verbose_name='d', null=True, blank=True) class Meta: unique_together = ('field_c', 'field_d',) I've run proper migration and

Django unique_together with nullable ForeignKey

自作多情 提交于 2019-11-29 05:39:42
I'm using Django 1.8.4 in my dev machine using Sqlite and I have these models: class ModelA(Model): field_a = CharField(verbose_name='a', max_length=20) field_b = CharField(verbose_name='b', max_length=20) class Meta: unique_together = ('field_a', 'field_b',) class ModelB(Model): field_c = CharField(verbose_name='c', max_length=20) field_d = ForeignKey(ModelA, verbose_name='d', null=True, blank=True) class Meta: unique_together = ('field_c', 'field_d',) I've run proper migration and registered them in the Django Admin. So, using the Admin I've done this tests: I'm able to create ModelA records

Custom form validation

主宰稳场 提交于 2019-11-27 17:10:26
I have a pretty simple form: from django import forms class InitialSignupForm(forms.Form): email = forms.EmailField() password = forms.CharField(max_length=255, widget = forms.PasswordInput) password_repeat = forms.CharField(max_length=255, widget = forms.PasswordInput) def clean_message(self): email = self.clean_data.get('email', '') password = self.clean_data.get('password', '') password_repeat = self.clean_data.get('password_repeat', '') try: User.objects.get(email_address = email) raise forms.ValidationError("Email taken.") except User.DoesNotExist: pass if password != password_repeat: