Advertisement
horozov86

relations_retake

Nov 30th, 2023
737
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | None | 0 0
  1. # 4.    License
  2.  
  3. class Driver(models.Model):
  4.     first_name = models.CharField(max_length=50)
  5.     last_name = models.CharField(max_length=50)
  6.    
  7. class DrivingLicense(models.Model):
  8.     license_number = models.CharField(max_length=10, unique=True)
  9.     issue_date = models.DateField()
  10.     driver = models.OneToOne("Driver", on_delete=models.CASCADE)
  11.    
  12.     def __str__(self):
  13.         expiration_date = self.issue_date + timedelta(days=365)
  14.         return f"License with id: {self.id} expires on {expiration_date}!
  15.    
  16.  
  17. def calculate_licenses_expiration_dates():
  18.    licenses = DrivingLicense.objects.order_by('-license_number')
  19.    
  20.    return "\n".join(str(l) for l in licenses)
  21.    
  22. def get_drivers_with_expired_licenses(due_date):
  23.    expiration_cutoff_date = due_date - timedelta(days=365)
  24.    
  25.    expired_drivers = Driver.objects.filter(DrivingLicense__issue_date__gt=expiration_cutoff_date)
  26.    
  27.    return expired_drivers
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement