Hello, this is one comes from djaunty_project.
I have virtually finish all the task, but i have trouble with interpreting the purpose of task number 13
12.
You can now implement the additional charges.
Add the following conditionals:
If self.bike‘s .bike_type is "TA" , then increase curr_price by TANDEM_SURCHARGE.
If self.bike‘s .bike_type is "EL" , then increase curr_price by ELECTRIC_SURCHARGE.
If self.renter‘s .vip_num is greater than 0, then discount curr_price by 20%.
Stuck? Get a hint
13.
Still within the .calc_price() method, finalize the cost of the Rental instance by setting the .price field as curr_price.
How .price is bind to the model instance? is this become new field or only exist only when called?
The latter seems obviously weird, but in Custom Model Methods there are no actual example of calling the method within the class or after instance is created (The example mentioned is implicit for reverse relationship, but never discussed how it is called etc).
I commented out the last two lines in the following code to suggest my intention (fail because ‘self’ is not recognized, how is that possible?). Maybe i misinterpret what the task purpose. Can anyone show me the hint? thank you very much.
from django.db import models
import datetime
BASE_PRICE = 25.00
TANDEM_SURCHARGE = 15.00
ELECTRIC_SURCHARGE = 25.00
# Create your models here.
class Bike(models.Model):
STANDARD = "ST"
TANDEM = "TA"
ELECTRIC = "EL"
BIKE_TYPE_CHOICES = [(STANDARD, "Standard"),
(TANDEM, "Tandem"),
(ELECTRIC, "Electric")]
bike_type = models.CharField(max_length=2,choices=BIKE_TYPE_CHOICES,default=STANDARD)
color = models.CharField(max_length=10, blank=True)
def __str__(self):
return self.bike_type + " - " + self.color
class Renter(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
phone = models.CharField(max_length=15)
vip_num = models.IntegerField(default=0)
def __str__(self):
self.first_name + " " + self.last_name + " - #" + self.phone
class Rental(models.Model):
bike = models.ForeignKey(Bike, on_delete=models.CASCADE)
renter = models.ForeignKey(Renter, on_delete=models.CASCADE)
date = models.DateField(default=datetime.date.today)
def calc_price(self):
curr_price = BASE_PRICE
if self.bike.bike_type == "TA":
curr_price += TANDEM_SURCHARGE
elif self.bike.bike_type == "EL":
curr_price += ELECTRIC_SURCHARGE
elif self.renter.vip_num > 0:
curr_price *= 1.2
self.price = curr_price
return self.price
#price = self.calc_price()
#price = models.IntegerField(default=price)