def update(self, instance, validated_data): choice_data = validated_data.pop('attributechoice_set') # We keep a list of our Choice id's so we can delete any choice that # is not included in the request. choice_ids = [] for choice in choice_data: # If id is within the call, then update the object with matching id if 'id' in choice: try: choice_obj = AttributeChoice.objects.get(pk=choice['id']) choice_obj.value = choice['value'] choice_obj.slug = choice['slug'] choice_obj.attribute = instance # If ID is not found, then create a new object except AttributeChoice.DoesNotExist: choice.pop('id') choice_obj = AttributeChoice(**choice) # If no ID within the call, create a new object. else: choice_obj = AttributeChoice(**choice) choice_obj.save() choice_ids.append(choice_obj.id) # Now delete all other entries that was NOT updated. # Meaning... If there was any attributechoices NOT included in our call (= They've been deleted) # Then delete them from the database. for choice in instance.attributechoice_set.all(): if choice.id not in choice_ids: choice.delete() return instance
def create_attribute(self, instance, validated_data): choice_data = validated_data.pop('attributechoice_set') validated_data['slug'] = slugify(validated_data['name']) attribute = Attribute(**validated_data) attribute.save() for choice in choice_data: choice_obj = AttributeChoice(attribute=attribute, **choice) choice_obj.save() return attribute
def create(self, validated_data): # Seperate nested data... choice_data = validated_data.pop('attributechoice_set') # Create the Attribute with remaining data. attribute = Attribute(**validated_data) attribute.save() # Create the AttributeChoices with the nested data seperated above. for choice in choice_data: choice_obj = AttributeChoice(attribute=attribute, **choice) choice_obj.save() return attribute
def update_attribute(self, instance, validated_data): choice_data = validated_data.pop('attributechoice_set') choice_ids = [] validated_data['slug'] = slugify(validated_data['name']) # Update the existing values of the instance with the values from # the request, and save it to the database. for(key, value) in validated_data.items(): setattr(instance, key, value) instance.save() for choice in choice_data: # If an ID already exists it means that the entry should be UPDATED. if 'id' in choice: try: choice_obj = AttributeChoice.objects.get(pk=choice['id']) choice_obj.value = choice['value'] choice_obj.slug = slugify(choice['value']) choice_obj.attribute = instance # If no ID exists, it means that the entry should be CREATED. except AttributeChoice.DoesNotExist: choice.pop('id') choice_obj = AttributeChoice(**choice) # If no ID exists, it means that the entry should be CREATED. else: choice_obj = AttributeChoice(**choice) choice_obj.save() choice_ids.append(choice_obj.id) # Check existing entries in the database with the actual entries that were # included in the request. If they were NOT included in the request, it means # that the user removed them, and they should be deleted from the database. for choice in instance.attributechoice_set.all(): if choice.id not in choice_ids: choice.delete() return instance