Advertisement
pacho_the_python

Untitled

Jun 24th, 2023
766
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.04 KB | None | 0 0
  1. from django.core.exceptions import ValidationError
  2. from django.db import models
  3. from django.core.validators import MinLengthValidator, MinValueValidator
  4.  
  5.  
  6. def check_first_letter(value):
  7.     if not value[0].isalpha():
  8.         raise ValidationError("Your name must start with a letter!")
  9.  
  10.  
  11. def only_letters_check(value):
  12.     for ch in value:
  13.         if not ch.isalpha():
  14.             raise ValidationError("Fruit name should contain only letters!")
  15.  
  16.  
  17. class Profile(models.Model):
  18.     first_name = models.CharField(
  19.         verbose_name='First Name',
  20.         max_length=25,
  21.         validators=[MinLengthValidator(2), check_first_letter],
  22.         null=False,
  23.         blank=False,
  24.     )
  25.  
  26.     last_name = models.CharField(
  27.         verbose_name='Last Name',
  28.         max_length=35,
  29.         validators=[MinLengthValidator(1), check_first_letter],
  30.         null=False,
  31.         blank=False,
  32.     )
  33.  
  34.     email = models.CharField(
  35.         verbose_name='Email',
  36.         max_length=40,
  37.         null=False,
  38.         blank=False,
  39.     )
  40.  
  41.     password = models.CharField(
  42.         verbose_name='Password',
  43.         max_length=20,
  44.         validators=[MinLengthValidator(8)],
  45.         null=False,
  46.         blank=False,
  47.     )
  48.  
  49.     image_url = models.URLField(
  50.         verbose_name='Image URL',
  51.         null=True,
  52.         blank=True,
  53.     )
  54.  
  55.     age = models.IntegerField(
  56.         verbose_name='Age',
  57.         default=18,
  58.         null=True,
  59.         blank=True,
  60.     )
  61.  
  62.  
  63. class Fruit(models.Model):
  64.     name = models.CharField(
  65.         verbose_name='Name',
  66.         max_length=30,
  67.         validators=[MinLengthValidator(2), only_letters_check],
  68.         null=False,
  69.         blank=False,
  70.     )
  71.  
  72.     image_url = models.URLField(
  73.         verbose_name='Image URL',
  74.         null=False,
  75.         blank=False,
  76.     )
  77.  
  78.     description = models.TextField(
  79.         verbose_name='Description',
  80.         null=False,
  81.         blank=False,
  82.     )
  83.  
  84.     nutrition = models.TextField(
  85.         verbose_name='Nutrition',
  86.         null=True,
  87.         blank=True,
  88.     )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement