[bug] prevent changing of order creation_date via API (#13284)

This commit is contained in:
Oliver authored and GitHub committed 2026-11-15 07:31:21 +11:00
1 parent 19dfb81bfb
commit 9e78e73a7c
2 files changed
+20

No files matched your search

@@ -200,6 +200,16 @@ class AbstractOrderSerializer(
return value return value
def get_fields(self):
"""Dynamically set creation_date as read-only on update operations."""
fields = super().get_fields()
# Make creation_date read-only when updating an existing order
if self.instance is not None and 'creation_date' in fields:
fields['creation_date'].read_only = True
return fields
@staticmethod @staticmethod
def annotate_queryset(queryset): def annotate_queryset(queryset):
"""Add extra information to the queryset.""" """Add extra information to the queryset."""
+10
View File
@@ -634,6 +634,16 @@ class PurchaseOrderTest(OrderTest):
po = models.PurchaseOrder.objects.get(pk=response.data['pk']) po = models.PurchaseOrder.objects.get(pk=response.data['pk'])
self.assertEqual(po.creation_date, creation_date) self.assertEqual(po.creation_date, creation_date)
# Next, check that the creation_date is read-only on update
po_pk = po.pk
response = self.patch(
reverse('api-po-detail', kwargs={'pk': po_pk}),
{'creation_date': '2000-01-01'},
expected_code=200,
)
po.refresh_from_db()
self.assertEqual(po.creation_date, creation_date)
def test_po_duplicate(self): def test_po_duplicate(self):
"""Test that we can duplicate a PurchaseOrder via the API.""" """Test that we can duplicate a PurchaseOrder via the API."""
self.assignRole('purchase_order.add') self.assignRole('purchase_order.add')