Skip to content

Commit

Permalink
Fix error updating venue information (#48)
Browse files Browse the repository at this point in the history
* Add missing param to update_from_dict method

* Add validation for fields in update_from_dict method and handle ForeignKey assignments

* Fix syntax error in update_from_dict method for field existence check

* Fix ForeignKey assignment in TimestampedModel to ensure correct instance type
  • Loading branch information
Johnwz123 authored Jan 21, 2025
1 parent 55ae322 commit 375c135
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions backend/treeckle/treeckle/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ class TimestampedModel(models.Model):
class Meta:
abstract = True

def update_from_dict(self, update_dict):
def update_from_dict(self, update_dict, commit=True):
for field, value in update_dict.items():
setattr(self, field, value)
if not hasattr(self, field):
raise ValueError(f"Field '{field}' does not exist in model '{self.__class__.__name__}'")

field_instance = self._meta.get_field(field)
if isinstance(field_instance, models.ForeignKey):
related_model = field_instance.related_model
if not isinstance(value, related_model):
value = related_model.objects.get(pk=value)

setattr(self, field, value)

if commit:
self.save()

0 comments on commit 375c135

Please sign in to comment.