From df5e1673767c5728959a0c137a678f5b9e659bac Mon Sep 17 00:00:00 2001 From: Stephan Telling Date: Sun, 4 Mar 2018 16:26:35 +0100 Subject: [PATCH] add on_delete to all foreignkeys --- src/bar/models.py | 8 ++++++-- src/program/models.py | 6 +++++- src/shop/models.py | 12 ++++++++---- src/sponsors/models.py | 3 ++- src/teams/models.py | 5 +++-- src/tickets/models.py | 14 +++++++++----- src/villages/models.py | 4 ++-- 7 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/bar/models.py b/src/bar/models.py index 8d4d3b87..ce82f445 100644 --- a/src/bar/models.py +++ b/src/bar/models.py @@ -4,7 +4,7 @@ from utils.models import CampRelatedModel class ProductCategory(CampRelatedModel): name = models.CharField(max_length=255) - camp = models.ForeignKey('camps.Camp') + camp = models.ForeignKey('camps.Camp', on_delete=models.PROTECT) def __str__(self): return self.name @@ -16,7 +16,11 @@ class ProductCategory(CampRelatedModel): class Product(models.Model): name = models.CharField(max_length=255) price = models.IntegerField() - category = models.ForeignKey(ProductCategory, related_name="products") + category = models.ForeignKey( + ProductCategory, + related_name="products", + on_delete=models.PROTECT + ) in_stock = models.BooleanField(default=True) class Meta: diff --git a/src/program/models.py b/src/program/models.py index 43a0c8eb..c7e7b34b 100644 --- a/src/program/models.py +++ b/src/program/models.py @@ -655,7 +655,11 @@ class Speaker(CampRelatedModel): class Favorite(models.Model): - user = models.ForeignKey('auth.User', related_name='favorites') + user = models.ForeignKey( + 'auth.User', + related_name='favorites', + on_delete=models.PROTECT + ) event_instance = models.ForeignKey('program.EventInstance') class Meta: diff --git a/src/shop/models.py b/src/shop/models.py index a8d3acc7..3ec7c917 100644 --- a/src/shop/models.py +++ b/src/shop/models.py @@ -72,6 +72,7 @@ class Order(CreatedUpdatedModel): verbose_name=_('User'), help_text=_('The user this shop order belongs to.'), related_name='orders', + on_delete=models.PROTECT, ) paid = models.BooleanField( @@ -290,7 +291,8 @@ class Product(CreatedUpdatedModel, UUIDModel): category = models.ForeignKey( 'shop.ProductCategory', - related_name='products' + related_name='products', + on_delete=models.PROTECT, ) name = models.CharField(max_length=150) @@ -311,6 +313,7 @@ class Product(CreatedUpdatedModel, UUIDModel): ticket_type = models.ForeignKey( 'tickets.TicketType', + on_delete=models.PROTECT, null=True, blank=True ) @@ -345,8 +348,8 @@ class Product(CreatedUpdatedModel, UUIDModel): class OrderProductRelation(CreatedUpdatedModel): - order = models.ForeignKey('shop.Order') - product = models.ForeignKey('shop.Product') + order = models.ForeignKey('shop.Order', on_delete=models.PROTECT) + product = models.ForeignKey('shop.Product', on_delete=models.PROTECT) quantity = models.PositiveIntegerField() handed_out = models.BooleanField(default=False) @@ -374,7 +377,7 @@ class EpayPayment(CreatedUpdatedModel, UUIDModel): verbose_name_plural = 'Epay Payments' order = models.OneToOneField('shop.Order') - callback = models.ForeignKey('shop.EpayCallback') + callback = models.ForeignKey('shop.EpayCallback', on_delete=models.PROTECT) txnid = models.IntegerField() @@ -402,6 +405,7 @@ class CreditNote(CreatedUpdatedModel): verbose_name=_('User'), help_text=_('The user this credit note belongs to, if any.'), related_name='creditnotes', + on_delete=models.PROTECT, null=True, blank=True ) diff --git a/src/sponsors/models.py b/src/sponsors/models.py index f1823b99..bad3655f 100644 --- a/src/sponsors/models.py +++ b/src/sponsors/models.py @@ -19,7 +19,7 @@ class Sponsor(CampRelatedModel): help_text='Name of the sponsor' ) - tier = models.ForeignKey('sponsors.SponsorTier') + tier = models.ForeignKey('sponsors.SponsorTier', on_delete=models.PROTECT) description = models.TextField( help_text='A short description of the sponsorship' @@ -57,6 +57,7 @@ class SponsorTier(CampRelatedModel): camp = models.ForeignKey( 'camps.Camp', null=True, + on_delete=models.PROTECT, related_name='sponsor_tiers', help_text='The camp this sponsor tier belongs to', ) diff --git a/src/teams/models.py b/src/teams/models.py index 4e6353d1..5599f27a 100644 --- a/src/teams/models.py +++ b/src/teams/models.py @@ -106,8 +106,8 @@ class Team(CampRelatedModel): class TeamMember(CampRelatedModel): - user = models.ForeignKey('auth.User') - team = models.ForeignKey('teams.Team') + user = models.ForeignKey('auth.User', on_delete=models.PROTECT) + team = models.ForeignKey('teams.Team', on_delete=models.PROTECT) approved = models.BooleanField(default=False) responsible = models.BooleanField(default=False) @@ -132,6 +132,7 @@ class TeamTask(CampRelatedModel): team = models.ForeignKey( 'teams.Team', related_name='tasks', + on_delete=models.PROTECT, help_text='The team this task belongs to', ) name = models.CharField( diff --git a/src/tickets/models.py b/src/tickets/models.py index f81dc31e..31269e3b 100644 --- a/src/tickets/models.py +++ b/src/tickets/models.py @@ -18,14 +18,14 @@ logger = logging.getLogger("bornhack.%s" % __name__) # TicketType can be full week, one day. etc. class TicketType(CampRelatedModel, UUIDModel): name = models.TextField() - camp = models.ForeignKey('camps.Camp') + camp = models.ForeignKey('camps.Camp', on_delete=models.PROTECT) def __str__(self): return '{} ({})'.format(self.name, self.camp.title) class BaseTicket(CampRelatedModel, UUIDModel): - ticket_type = models.ForeignKey('TicketType') + ticket_type = models.ForeignKey('TicketType', on_delete=models.PROTECT) checked_in = models.BooleanField(default=False) badge_handed_out = models.BooleanField(default=False) @@ -69,7 +69,7 @@ class BaseTicket(CampRelatedModel, UUIDModel): class SponsorTicket(BaseTicket): - sponsor = models.ForeignKey('sponsors.Sponsor') + sponsor = models.ForeignKey('sponsors.Sponsor', on_delete=models.PROTECT) def __str__(self): return 'SponsorTicket: {}'.format(self.pk) @@ -93,8 +93,12 @@ class DiscountTicket(BaseTicket): class ShopTicket(BaseTicket): - order = models.ForeignKey('shop.Order', related_name='shoptickets') - product = models.ForeignKey('shop.Product') + order = models.ForeignKey( + 'shop.Order', + related_name='shoptickets', + on_delete=models.PROTECT + ) + product = models.ForeignKey('shop.Product', on_delete=models.PROTECT) name = models.CharField( max_length=100, diff --git a/src/villages/models.py b/src/villages/models.py index a6f8db8c..24b7685b 100644 --- a/src/villages/models.py +++ b/src/villages/models.py @@ -11,8 +11,8 @@ class Village(UUIDModel, CampRelatedModel): ordering = ['name'] unique_together = ('slug', 'camp') - contact = models.ForeignKey('auth.User') - camp = models.ForeignKey('camps.Camp') + contact = models.ForeignKey('auth.User', on_delete=models.PROTECT) + camp = models.ForeignKey('camps.Camp', on_delete=models.PROTECT) name = models.CharField(max_length=255) slug = models.SlugField(max_length=255, blank=True) description = models.TextField(