Questions tagged [django-validation]

django-validation refers to form and field validation tools provided by Django out of the box

django-validation refers to form and field validation tools provided by Django out of the box.

See documentation.

365 questions
221
votes
10 answers

How to limit the maximum value of a numeric field in a Django model?

Django has various numeric fields available for use in models, e.g. DecimalField and PositiveIntegerField. Although the former can be restricted to the number of decimal places stored and the overall number of characters stored, is there any way to…
user82216
215
votes
11 answers

What's the best way to store a phone number in Django models?

I am storing a phone number in model like this: phone_number = models.CharField(max_length=12) The user would enter a phone number and I would use the phone number for SMS authentication. This application would be used globally. So I would also…
pynovice
  • 7,424
  • 25
  • 69
  • 109
182
votes
7 answers

Why doesn't django's model.save() call full_clean()?

I'm just curious if anyone knows if there's good reason why django's orm doesn't call 'full_clean' on a model unless it is being saved as part of a model form. Note that full_clean() will not be called automatically when you call your model’s…
Aaron
  • 4,206
  • 3
  • 24
  • 28
58
votes
3 answers

Custom validation in Django admin

I have a very simple Django app in order to record the lectures given my colleagues.Since it is quite elementary,I am using the Django admin itself. Here is my models.py: #models.py from django.db import models class Lecture(models.Model): …
Amistad
  • 7,100
  • 13
  • 48
  • 75
54
votes
1 answer

Custom form validation

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 =…
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
52
votes
6 answers

Django unique_together with nullable ForeignKey

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: …
MatheusJardimB
  • 3,599
  • 7
  • 46
  • 70
43
votes
2 answers

Is save() called implicitly when calling create in django?

I'm trying to perform some custom validation on a model and I'm getting confused. Let me be specific. Let's say my code is as follows: class FooManager(models.Manager): def create_foo(self, name): return self.create(foo_name = name) class…
innospark
  • 778
  • 1
  • 5
  • 8
34
votes
3 answers

Django TextField max_length validation for ModelForm

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…
onurmatik
  • 5,105
  • 7
  • 42
  • 67
20
votes
5 answers

Adding css class to field on validation error in django

I am using Django's modelform and its really good. How can I highlight the actual text box (e.g. border:red ) if there is a validation error associated with it. Basically what i want is to add a class (error) if there is a validation error to a…
Myth
  • 1,562
  • 5
  • 15
  • 25
13
votes
3 answers

Django - form Clean() and field errors

I'm trying to set field errors in a form clean() and I'm currently doing: self._errors['address'] = self._errors.get('address', ErrorList()) self._errors['address'].append(_(u'Please specify an address.')) Is there a better and if possible shorter…
RS7
  • 2,341
  • 8
  • 34
  • 57
12
votes
3 answers

Django ModelChoiceField - use something other than id?

Say I have an address table and it has a postal_code field -- ModelChoiceField does not allow me to use something other than PKs to validate existence correct? What would be the way to go? Normal input and use clean_*()?
RS7
  • 2,341
  • 8
  • 34
  • 57
10
votes
1 answer

Cleanest Way to Allow Empty Scheme in Django URLField

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…
cmanbst
  • 179
  • 3
  • 13
10
votes
2 answers

Validation in patch method in django rest framework

Following is my model definition class ProfessionalQualification(Log_Active_Owned_Model_Mixin): PROF_TEACHER = 1 PROF_ENGINEER = 2 PROF_DOCTOR = 4 PROF_PROFESSOR = 8 PROF_MANAGER = 16 PROF_CLERK = 32 …
Mangu Singh Rajpurohit
  • 10,806
  • 4
  • 68
  • 97
10
votes
1 answer

How to write a Custom field Validation for ModelSerializers in Django Rest Framework(DRF) similar to Form Validation in Django?

I am currently creating an api based on DRF.I have a model which is like: class Task(models.Model): name = models.CharField(max_length = 255) completed = models.BooleanField(default = False) description = models.TextField() text =…
Rahul
  • 2,580
  • 1
  • 20
  • 24
10
votes
1 answer

Django field validation in Model and in Admin?

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…
Al Bundy
  • 1,599
  • 1
  • 14
  • 20
1
2 3
24 25