django-validation

Django showing error 'constraints' refers to the joined field

Django showing error 'constraints' refers to the joined field Question: I have two models Product and Cart. Product model has maximum_order_quantity. While updating quantity in cart, I’ll have to check whether quantity is greater than maximum_order_quantityat database level. For that am comparing quantity with maximum_order_quantity in Cart Model But it throws an error when I …

Total answers: 1

python django serializer wrong date time format for DateTimeField

python django serializer wrong date time format for DateTimeField Question: I’m using Django 3.0.2. I have a serializer defined: class ValueNestedSerializer(request_serializer.Serializer): lower = request_serializer.DateTimeField(required=True, allow_null=False, format=None, input_formats=[‘%Y-%m-%dT%H:%M:%SZ’,]) upper = request_serializer.DateTimeField(required=True, allow_null=False, format=None, input_formats=[‘%Y-%m-%dT%H:%M:%SZ’,]) class DateRangeSerializer(request_serializer.Serializer): attribute = request_serializer.CharField(default="UPLOAD_TIME") operator = request_serializer.CharField(default="between_dates") value = ValueNestedSerializer(required=True) timezone = request_serializer.CharField(default="UTC") timezoneOffset = request_serializer.IntegerField(default=0) class BaseQueryPayload(request_serializer.Serializer): appid = request_serializer.CharField(required=True, …

Total answers: 1

Custom Validate function is not being called inside perform_create function in DRF

Custom Validate function is not being called inside perform_create function in DRF Question: This is my code. class MyViewSet(ModelViewSet): serializer_class = MySerializer queryset = MyClass.objects.all() def get_serializer_class(self): if request.user.is_superuser: return self.serializer_class else: return OtherSerializer def perform_create(self, serializer): if request.user.is_superuser: if serializer.is_valid(): serializer.save(organization=self.request.user.organization) else: employee = Employee.objects.get(user=self.request.user) serializer.save(employee=employee, organization=self.request.user.organization) This is my Serializer: class MySerializer(serializers.ModelSerializer): class …

Total answers: 1

RegexValidator in Django Models not validating email correctly

RegexValidator in Django Models not validating email correctly Question: I’m making a django form with an email field and using a RegexValidator and want a specific format of of the email but it seems to be not validating the field correctly email = models.EmailField( unique=True, validators=[ RegexValidator( regex=r"^[2][2][a-zA-Z]{3}d{3}@[nith.ac.in]*", message= "Only freshers with college email addresses …

Total answers: 1

How to write unit test ValidationError case in response by use client.post()?

How to write unit test ValidationError case in response by use client.post()? Question: I have a model with a time validator raise ValidationError(‘End time cannot be earlier than start time’) So I want to write a unit test using client.post() with data invalid (from_time > to_time), and I expected ValidationError to appear in this test. …

Total answers: 2

Model a 6-digit numeric postal code in django

Model a 6-digit numeric postal code in django Question: I would like to define a 6-digit numeric postal code in Django models.py. At first, I tried this; postal_code = models.PositiveIntegerField(blank=False) However, postal codes can have leading zeros such as 000157. PositiveIntegerField is not suitable for it. If I use CharField, the field can accept alphabets …

Total answers: 1

Django DecimalField min value validation error

Django DecimalField min value validation error Question: I need a decimal field in my form with a minimal value of 0.20, maximal value of 20 with 2 decimal places as you can see in the code bellow. forms.DecimalField(min_value=0.20, max_value=20.00, max_digits=4, decimal_places=2, initial=1) The problem occures when you put 0.20 in, because you get a validation …

Total answers: 2

Which is the best way to validate errors in Django?

Which is the best way to validate errors in Django? Question: I found that there is a lot of ways to validate errors in Django, I’m just wondering which one is preferable and why? e.g. using clean in form, like: deadline = self.cleaned_data.get(‘deadline’) if datetime.strptime(str(deadline)[:19], ‘%Y-%m-%d %H:%M:%S’) < datetime.today(): raise forms.ValidationError(_(‘Deadline cannot be in past!’)) …

Total answers: 2

Custom validation in Django admin

Custom validation in Django admin Question: 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): topic = models.CharField(max_length=100) speaker = models.CharField(max_length=100) start_date = models.DateField() end_date = models.DateField() …

Total answers: 3

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

What's the best way to store a phone number in Django models? Question: 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 need a …

Total answers: 10