Advertisement
Sichanov

photos models

Oct 16th, 2022
1,190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.51 KB | None | 0 0
  1. from django.core.validators import MinLengthValidator
  2. from django.db import models
  3.  
  4. from petstagram.core.model_mixins import StrFromFieldsMixin
  5. from petstagram.pets.models import Pet
  6. from petstagram.photos.validators import validate_file_less_than_5mb
  7.  
  8.  
  9. class Photo(StrFromFieldsMixin, models.Model):
  10.     str_fields = ('photo', 'location')
  11.     MIN_DESCRIPTION_LENGTH = 10
  12.     MAX_DESCRIPTION_LENGTH = 300
  13.  
  14.     MAX_LOCATION_LENGTH = 30
  15.  
  16.     # Requires mediafiles to work correctly
  17.     photo = models.ImageField(
  18.         upload_to='mediafiles/pet_photos/',
  19.         null=False,
  20.         blank=True,
  21.         validators=(validate_file_less_than_5mb,),
  22.  
  23.     )
  24.  
  25.     description = models.CharField(
  26.         # DB validation
  27.         max_length=MAX_DESCRIPTION_LENGTH,
  28.         validators=(
  29.             # Django/python validation, not DB validation
  30.             MinLengthValidator(MIN_DESCRIPTION_LENGTH),
  31.         ),
  32.         null=True,
  33.         blank=True,
  34.     )
  35.  
  36.     location = models.CharField(
  37.         max_length=MAX_LOCATION_LENGTH,
  38.         null=True,
  39.         blank=True,
  40.     )
  41.  
  42.     publication_date = models.DateField(
  43.         # Automatically sets current date on 'save' (update or create)
  44.         # auto_now_add sets current date on 'save' FIRST TIME ONLY
  45.         auto_now=True,
  46.         null=False,
  47.         blank=True,
  48.  
  49.     )
  50.  
  51.     # One-to-one relations
  52.  
  53.     # One-to-many relations
  54.  
  55.     # Many-to-many relations
  56.  
  57.     tagged_pets = models.ManyToManyField(
  58.         Pet,
  59.         blank=True,
  60.     )
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement