[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
- Send PATCH to /api/locations/{id}/ with visits data:
{
"name": "Updated Location",
"visits": [{"date": "2025-10-10", "notes": "Great visit"}]
} - 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.