Fix: Auto-calculate SlotEntry.order when not provided via API

The SlotEntry model's `order` field was defined with `blank=True` but no
default value, causing 500 Internal Server Error when creating entries
via /api/v2/slot-entry/ without specifying an order.

Changes:
- Add `null=True` to allow Python None value before save
- Auto-calculate order in save() method as max(existing_orders) + 1

This matches the behavior of similar models (Day, Slot) which have
default=1 for their order fields.

Fixes: IntegrityError "null value in column 'order' violates not-null constraint"

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
ndimoro
2026-01-23 13:39:39 -07:00
parent 9262550a04
commit 677da4427b

View File

@@ -140,6 +140,7 @@ class SlotEntry(models.Model):
order = models.PositiveIntegerField(
blank=True,
null=True,
db_index=True,
)
@@ -213,6 +214,12 @@ class SlotEntry(models.Model):
)
if not self.weight_rounding:
self.weight_rounding = self.slot.day.routine.user.userprofile.weight_rounding
# Auto-calculate order if not provided
if self.order is None:
max_order = self.slot.entries.aggregate(models.Max('order'))['order__max']
self.order = (max_order or 0) + 1
return super().save(*args, **kwargs)
def get_owner_object(self):