[Answered ]-How to Update a field in Django

1👍

You can construct a dictionary that only contains the key-value pairs to update and then use .update(…) [Django-doc] to update the corresponding Category record:

categories = Category.objects.filter(id=data['id'])
updates = {}

if 'name' in data:
    updates['name'] = data['name']
if 'image' in data:
    updates['image'] = data['image']

if updates:
    categories.update(**updates)

Leave a comment