seanmorley15/AdventureLog

[BUG] Cannot update Location with visits data via PATCH

Opened this issue ยท 0 comments

๐Ÿ›‘ Note: Please search existing issues before filing a new one. This helps avoid duplicates and ensures we can address your concern efficiently!

๐Ÿž Bug Description

PATCH requests to update a location with visits data fail with:

TypeError: Direct assignment to the reverse side of a related set is prohibited. Use visits.set() instead.

๐Ÿ”„ Steps to Reproduce

  1. Send PATCH to /api/locations/{id}/ with visits data:
    {
    "name": "Updated Location",
    "visits": [{"date": "2025-10-10", "notes": "Great visit"}]
    }
  2. Observe TypeError

โœ… Expected Behavior

Location and visits should update successfully.

๐Ÿ“ธ Screenshots / Logs

Root Cause:
LocationSerializer.update() (line ~385) doesn't extract visits from validated_data before the setattr loop, causing direct assignment to a reverse relation.

Fix (Claude suggestion):
def update(self, instance, validated_data):
visits_data = validated_data.pop('visits', None) # Add this
category_data = validated_data.pop('category', None)
collections_data = validated_data.pop('collections', None)

for attr, value in validated_data.items():
    setattr(instance, attr, value)
instance.save()

# Handle visits after save
if visits_data is not None:
    instance.visits.clear()
    for visit_data in visits_data:
        Visit.objects.create(location=instance, **visit_data)

return instance

๐Ÿณ Environment Details

  • Platform: N/A (Backend API issue)
  • AdventureLog Version: Latest dev
  • Component: LocationSerializer backend

๐Ÿ“Ž Additional Context

Discovered by Claude Code during mobile app development. The serializer already handles category and collections this way, but visits was missed.