Advertisement
pacho_the_python

Untitled

Oct 19th, 2022
828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.28 KB | None | 0 0
  1. from django.db import models
  2. from django.core.validators import MinValueValidator, ValidationError
  3.  
  4. # Create your models here.
  5.  
  6. GENRE_CHOICES = (
  7.         ('Pop Music', 'Pop Music'),
  8.         ("Jazz Music", "Jazz Music"),
  9.         ("R&B Music", "R&B Music"),
  10.         ("Rock Music", "Rock Music"),
  11.         ("Country Music", "Country Music"),
  12.         ("Dance Music", "Dance Music"),
  13.         ("Hip Hop Music", "Hip Hop Music"),
  14.         ("Other", "Other")
  15.     )
  16.  
  17.  
  18. def username_validator(value):
  19.     for char in value:
  20.         if not char.isdigit() and not char.isalpha() and not char == '_':
  21.             raise ValidationError("Ensure this value contains only letters, numbers, and underscore.")
  22.  
  23.  
  24. class ProfileModel(models.Model):
  25.     USERNAME_MAX_LEN = 15
  26.     USERNAME_MIN_LEN = 2
  27.  
  28.     username = models.CharField(
  29.         verbose_name=' User Name',
  30.         max_length=USERNAME_MAX_LEN,
  31.         unique=True,
  32.         null=False,
  33.         blank=False,
  34.         validators=[MinValueValidator(USERNAME_MIN_LEN), username_validator]
  35.     )
  36.  
  37.     user_email = models.EmailField(
  38.         verbose_name="Email",
  39.         null=False,
  40.         blank=False,
  41.         unique=True,
  42.     )
  43.  
  44.     age = models.PositiveIntegerField(
  45.         verbose_name="Age",
  46.         null=True,
  47.         blank=True
  48.     )
  49.  
  50.  
  51. class AlbumModel(models.Model):
  52.     ALBUM_MAX_LEN = 30
  53.     ARTIST_MAX_LEN = 30
  54.     GENRE_MAX_LEN = 30
  55.  
  56.     album = models.CharField(
  57.         verbose_name='Album',
  58.         max_length=ALBUM_MAX_LEN,
  59.         unique=True,
  60.         null=False,
  61.         blank=False
  62.     )
  63.  
  64.     artist = models.CharField(
  65.         verbose_name='Artist',
  66.         max_length=ARTIST_MAX_LEN,
  67.         null=False,
  68.         blank=False
  69.     )
  70.  
  71.     genre = models.CharField(
  72.         null=False,
  73.         blank=False,
  74.         verbose_name='Genre',
  75.         max_length=GENRE_MAX_LEN,
  76.         choices=GENRE_CHOICES
  77.     )
  78.  
  79.     description = models.TextField(
  80.         verbose_name='Description',
  81.         null=True,
  82.         blank=True
  83.     )
  84.  
  85.     image_url = models.URLField(
  86.         verbose_name='Image URL:',
  87.         null=False,
  88.         blank=False
  89.     )
  90.  
  91.     price = models.FloatField(
  92.         verbose_name='Price',
  93.         null=False,
  94.         blank=False,
  95.         validators=[MinValueValidator(0.0)]
  96.     )
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement