@@ -462,7 +462,7 @@ class Occurrence(models.Model):
462462 original_end = models .DateTimeField (("original end" ))
463463 created_on = models .DateTimeField (("created on" ), auto_now_add = True )
464464 updated_on = models .DateTimeField (("updated on" ), auto_now = True )
465- interval = models .IntegerField (("interval" ), blank = True , validators = [MinValueValidator (5 ), MaxValueValidator (60 )])
465+ interval = models .IntegerField (("interval" ), blank = True , null = True , validators = [MinValueValidator (5 ), MaxValueValidator (60 )])
466466
467467 class Meta :
468468 verbose_name = ("occurrence" )
@@ -480,23 +480,43 @@ def __init__(self, *args, **kwargs):
480480 self .location = event .location
481481
482482 def save (self , * args , ** kwargs ):
483+ # Get the old instance if this is an update
484+ old_instance = None
485+ if self .pk :
486+ old_instance = Occurrence .objects .get (pk = self .pk )
487+
483488 super ().save (* args , ** kwargs )
484489
485- if self .pk : # If save is called on object update, not creation
486- self .bookings .all ().delete ()
487-
488- delta = self .end - self .start
489- delta_minutes = delta .total_seconds () / 60
490- booking_count = int (delta_minutes // self .interval )
491- for i in range (booking_count ):
492- booking_start = self .start + timedelta (minutes = i * self .interval )
493- booking_end = booking_start + timedelta (minutes = self .interval )
494- Booking .objects .create (
495- occurrence = self ,
496- user = None ,
497- start = booking_start ,
498- end = booking_end ,
499- )
490+ # Only update bookings if:
491+ # 1. This is a new occurrence
492+ # 2. The interval has changed
493+ # 3. The start or end time has changed
494+ should_update_bookings = (
495+ not old_instance or # New occurrence
496+ old_instance .interval != self .interval or # Interval changed
497+ old_instance .start != self .start or # Start time changed
498+ old_instance .end != self .end # End time changed
499+ )
500+
501+ if should_update_bookings and self .interval :
502+ # Delete existing bookings
503+ if self .pk :
504+ self .bookings .all ().delete ()
505+
506+ # Create new bookings
507+ delta = self .end - self .start
508+ delta_minutes = delta .total_seconds () / 60
509+ booking_count = int (delta_minutes // self .interval )
510+
511+ for i in range (booking_count ):
512+ booking_start = self .start + timedelta (minutes = i * self .interval )
513+ booking_end = booking_start + timedelta (minutes = self .interval )
514+ Booking .objects .create (
515+ occurrence = self ,
516+ user = None ,
517+ start = booking_start ,
518+ end = booking_end ,
519+ )
500520
501521 def moved (self ):
502522 return self .original_start != self .start or self .original_end != self .end
0 commit comments