brew_journal/recipies/serializers.py
def update(self, instance, validated_data):
        """Update a recipe. This will clear all previous malts/hops and replace them with a new list"""

        instance.recipe_name    = validated_data.get('recipe_name',    instance.recipe_name)
        instance.recipe_style   = validated_data.get('recipe_style',   instance.recipe_style)
        instance.recipe_notes   = validated_data.get('recipe_notes',   instance.recipe_notes)
        instance.last_brew_date = validated_data.get('last_brew_date', instance.last_brew_date)

        new_hops  = validated_data.get('recipe_hops', instance.recipe_hops)
        new_malts = validated_data.get('recipe_malts', instance.recipe_malts)
        new_steps = validated_data.get('recipe_steps', instance.recipe_steps)

        # Delete all of the hops and then resave them
        instance.recipe_hops.all().delete()
        for hop in new_hops:
            hop['recipe'] = instance.id
            serialized_hop = RecipeHopsSerializer(data=hop)
            serialized_hop.is_valid(raise_exception=True) # Throw an exception if invalid
            serialized_hop.save()

        # Do the same for malts
        instance.recipe_malts.all().delete()
        for malt in new_malts:
            malt['recipe'] = instance.id
            serialized_malt = RecipeMaltsSerializer(data=malt)
            serialized_malt.is_valid(raise_exception=True)
            serialized_malt.save()

        instance.save()

        return instance
Update a recipe. This will clear all previous malts/hops and replace them with a new list
Update a recipe. This will clear all previous malts/hops and replace them with a new list
Update a recipe . This will clear all previous malts / hops and replace them with a new list
def update(self, instance, validated_data):
        instance.recipe_name    = validated_data.get('recipe_name',    instance.recipe_name)
        instance.recipe_style   = validated_data.get('recipe_style',   instance.recipe_style)
        instance.recipe_notes   = validated_data.get('recipe_notes',   instance.recipe_notes)
        instance.last_brew_date = validated_data.get('last_brew_date', instance.last_brew_date)
        new_hops  = validated_data.get('recipe_hops', instance.recipe_hops)
        new_malts = validated_data.get('recipe_malts', instance.recipe_malts)
        new_steps = validated_data.get('recipe_steps', instance.recipe_steps)
        instance.recipe_hops.all().delete()
        for hop in new_hops:
            hop['recipe'] = instance.id
            serialized_hop = RecipeHopsSerializer(data=hop)
            serialized_hop.is_valid(raise_exception=True) 
            serialized_hop.save()
        instance.recipe_malts.all().delete()
        for malt in new_malts:
            malt['recipe'] = instance.id
            serialized_malt = RecipeMaltsSerializer(data=malt)
            serialized_malt.is_valid(raise_exception=True)
            serialized_malt.save()
        instance.save()
        return instance
def update ( self , instance , validated_data ) : instance . recipe_name = validated_data . get ( 'recipe_name' , instance . recipe_name ) instance . recipe_style = validated_data . get ( 'recipe_style' , instance . recipe_style ) instance . recipe_notes = validated_data . get ( 'recipe_notes' , instance . recipe_notes ) instance . last_brew_date = validated_data . get ( 'last_brew_date' , instance . last_brew_date ) new_hops = validated_data . get ( 'recipe_hops' , instance . recipe_hops ) new_malts = validated_data . get ( 'recipe_malts' , instance . recipe_malts ) new_steps = validated_data . get ( 'recipe_steps' , instance . recipe_steps ) instance . recipe_hops . all ( ) . delete ( ) for hop in new_hops : hop [ 'recipe' ] = instance . id serialized_hop = RecipeHopsSerializer ( data = hop ) serialized_hop . is_valid ( raise_exception = True ) serialized_hop . save ( ) instance . recipe_malts . all ( ) . delete ( ) for malt in new_malts : malt [ 'recipe' ] = instance . id serialized_malt = RecipeMaltsSerializer ( data = malt ) serialized_malt . is_valid ( raise_exception = True ) serialized_malt . save ( ) instance . save ( ) return instance
Update a recipe.
Update a recipe .
"""Update a recipe. This will clear all previous malts/hops and replace them with a new list""" # Delete all of the hops and then resave them
 # Throw an exception if invalid
 # Do the same for malts

self
instance
validated_data