models.py 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. from django.db import models
  2. import datetime
  3. from django.core.validators import MaxValueValidator, MinValueValidator
  4. def current_year():
  5. return datetime.date.today().year
  6. def max_value_current_year(value):
  7. return MaxValueValidator(current_year())(value)
  8. class Autor(models.Model):
  9. nombre = models.CharField(max_length=200)
  10. biografia = models.TextField(blank=True, null=True)
  11. foto = models.ImageField(upload_to='autores/', blank=True, null=True) # Nuevo campo
  12. def __str__(self):
  13. return self.nombre
  14. class Libro(models.Model):
  15. titulo = models.CharField(max_length=200)
  16. autor = models.ForeignKey(Autor, on_delete=models.CASCADE)
  17. fecha_publicacion = models.PositiveBigIntegerField(default=current_year(), validators=[MinValueValidator(1984), max_value_current_year])
  18. descripcion = models.TextField(blank=True, null=True)
  19. archivo = models.FileField(upload_to='libros/')
  20. portada = models.ImageField(upload_to='portadas/', blank=True, null=True) # Nuevo campo
  21. def __str__(self):
  22. return self.titulo