Commit 0e371d5e authored by Alia Lawraine Olay's avatar Alia Lawraine Olay

Added ManytoManyField in Booking model

parent d44dc515
...@@ -42,7 +42,7 @@ class Booking_SchedAdmin(admin.ModelAdmin): ...@@ -42,7 +42,7 @@ class Booking_SchedAdmin(admin.ModelAdmin):
class Booking_AddItemAdmin(admin.ModelAdmin): class Booking_AddItemAdmin(admin.ModelAdmin):
model = Booking_AddItem model = Booking_AddItem
list_display = ('Booking_ID', 'AddItem_Desc', 'Quantity', 'subtotal') list_display = ('Booking_ID', 'AddItem_Desc', 'Quantity', 'Subtotal')
class PilotAdmin(admin.ModelAdmin): class PilotAdmin(admin.ModelAdmin):
model = Pilot model = Pilot
......
# Generated by Django 4.0.4 on 2022-12-01 05:22
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('booking', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='booking',
name='Additional_Item',
field=models.ManyToManyField(through='booking.Booking_AddItem', to='booking.additional_item'),
),
]
...@@ -31,23 +31,25 @@ class Passenger(models.Model): ...@@ -31,23 +31,25 @@ class Passenger(models.Model):
def __str__(self): def __str__(self):
return self.First_Name + ' ' + self.Last_Name return self.First_Name + ' ' + self.Last_Name
class Additional_Item(models.Model):
Description = models.CharField(max_length=50, primary_key=True, editable=True, unique=True)
Item_Cost = models.IntegerField(default=0)
def __str__(self):
return self.Description
class Booking(models.Model): class Booking(models.Model):
Booking_ID = models.AutoField(primary_key=True, editable=False, unique=True) Booking_ID = models.AutoField(primary_key=True, editable=False, unique=True)
Booking_Date = models.DateField(null=True, blank=True) Booking_Date = models.DateField(null=True, blank=True)
Total_Cost = models.IntegerField(blank=True, default=0,null=False) Total_Cost = models.IntegerField(blank=True, default=0,null=False)
Passenger = models.ForeignKey(Passenger, on_delete=models.CASCADE) Passenger = models.ForeignKey(Passenger, on_delete=models.CASCADE)
Additional_Item = models.ManyToManyField(Additional_Item, through='Booking_AddItem')
def __str__(self): def __str__(self):
return str(self.Booking_ID) return str(self.Booking_ID)
class Additional_Item(models.Model):
Description = models.CharField(max_length=50, primary_key=True, editable=True, unique=True)
Item_Cost = models.IntegerField(default=0)
def __str__(self):
return self.Description
class Booking_AddItem(models.Model): class Booking_AddItem(models.Model):
class Meta: class Meta:
...@@ -56,13 +58,14 @@ class Booking_AddItem(models.Model): ...@@ -56,13 +58,14 @@ class Booking_AddItem(models.Model):
Booking_ID = models.ForeignKey(Booking, on_delete=models.CASCADE) Booking_ID = models.ForeignKey(Booking, on_delete=models.CASCADE)
AddItem_Desc = models.ForeignKey(Additional_Item, on_delete=models.CASCADE) AddItem_Desc = models.ForeignKey(Additional_Item, on_delete=models.CASCADE)
Quantity = models.IntegerField(default=0) Quantity = models.IntegerField(default=0)
Subtotal = models.IntegerField(default=0, editable=False)
@property @property
def subtotal(self): def save(self):
add_item = Additional_Item.objects.get(Description = self.AddItem_Desc) add_item = Additional_Item.objects.get(Description = self.AddItem_Desc)
itemcost = add_item.Item_Cost itemcost = add_item.Item_Cost
subtotal = itemcost*self.Quantity self.subtotal = itemcost*self.Quantity
return subtotal return super().save()
......
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment