Advertisement
EricPy

models.py

Sep 3rd, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | None | 0 0
  1. import uuid # Required for unique car instances
  2.  
  3. from django.db import models
  4. from django.urls import reverse # Used to generate URLs by reversing the URL patterns
  5.  
  6. # Create your models here.
  7. class Brand(models.Model):
  8.     """Model representing a car's brand/manufacturer"""      
  9.     name = models.CharField(max_length=200, help_text='Enter a car brand (e.g. Toyota)')
  10.     logo = models.ImageField(upload_to='media/images/logos', help_text='Upload brand logo')
  11.  
  12.     def __str__(self):
  13.         """String for representing the Model object."""
  14.         return self.name
  15.    
  16.     def display_quantity(self):
  17.         """Return the quantity of cars of this brand"""
  18.         return self.__class__.objects.count()
  19.  
  20.     display_quantity.short_description = 'Quantity'
  21.  
  22.  
  23. class Car(models.Model):
  24.     """Model representing a car (but not a specific copy of a car)."""
  25.     name = models.CharField(max_length=200)
  26.  
  27.     # Foreign Key used because car can only have one brand, but brands can have multiple cars
  28.     # car as a string rather than object because it hasn't been declared yet in the file
  29.     brand = models.ForeignKey('Brand', on_delete=models.SET_NULL, null=True, help_text='Enter the specific model of the car')
  30.     price = models.IntegerField(help_text='Enter price of the car')
  31.    
  32.     summary = models.TextField(max_length=1000, help_text='Enter a brief description of the car')
  33.    
  34.     photo = models.ImageField(upload_to='media/images/cars', help_text='Upload the image of the car')
  35.    
  36.     def __str__(self):
  37.         """String for representing the Model object."""
  38.         return self.name
  39.    
  40.     def get_absolute_url(self):
  41.         """Returns the url to access a detail record for this car."""
  42.         return reverse('car-detail', args=[str(self.id)])
  43.    
  44.     def display_quantity(self):
  45.         """Return the quantity of a specific car's instances"""
  46.         return self.__class__.objects.count()
  47.  
  48.     display_quantity.short_description = 'Quantity'
  49.  
  50.  
  51. class CarInstance(models.Model):
  52.     """Model representing a specific copy of a car (i.e. that can be bought from the shop)."""
  53.     id = models.UUIDField(primary_key=True, default=uuid.uuid4, help_text='Unique ID for this particular car across whole shop')
  54.     car = models.ForeignKey('Car', on_delete=models.SET_NULL, null=True)
  55.  
  56.     PURCHASE_STATUS = (
  57.         ('a', 'Available'),
  58.         ('s', 'Sold Out'),
  59.     )
  60.  
  61.     status = models.CharField(
  62.         max_length=1,
  63.         choices=PURCHASE_STATUS,
  64.         blank=True,
  65.         default='s',
  66.         help_text='Car availability',
  67.     )
  68.  
  69.     class Meta:
  70.         ordering = ['status']
  71.  
  72.     def __str__(self):
  73.         """String for representing the Model object."""
  74.         return f'{self.id} ({self.car.name})'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement