def update(self, instance, validated_data): instance.user = validated_data.get('user', instance.user) instance.save() if 'ingredients' in validated_data: # Delete any quantified ingredients not included in the request. quantified_ingredient_ids = [item.get('id') for item in validated_data['ingredients']] for quantified_ingredient in instance.ingredients.all(): if quantified_ingredient.id not in quantified_ingredient_ids: quantified_ingredient.delete() # Create or update quantified ingredient instances that are in the request. for item in validated_data['ingredients']: if item.get('units') is not None: units = Unit(**item['units']) units.save() else: units = None name = Ingredient(**item['name']) name.save() quantified_ingredient = QuantifiedIngredient(id=item.get('id'), qty=item.get('qty'), name=name, is_checked=item.get('is_checked', QuantifiedIngredient._meta.get_field('is_checked').get_default()), units=units, grocery_list=instance, original_recipe=item.get('original_recipe')) quantified_ingredient.save() return instance
def handle(self, *args, **options): reader = csv.DictReader( open("/opt/app/nomnom/management/commands/Zutatenliste.csv", 'r')) dict_list = [] for line in reader: dict_list.append(line['name']) print(dict_list) for ingredient in dict_list: ingred = Ingredient(name=ingredient) ingred.save()
def update(self, instance, validated_data): if validated_data.get('units') is not None: units = Unit(**validated_data['units']) units.save() else: units = None name = Ingredient(**validated_data['name']) name.save() instance.units = units instance.name = name instance.qty = validated_data.get('qty', instance.qty) instance.grocery_list = validated_data.get('grocery_list', instance.grocery_list) instance.recipe = validated_data.get('recipe', instance.recipe) instance.original_recipe = validated_data.get('original_recipe', instance.original_recipe) instance.is_checked = validated_data.get('is_checked', instance.is_checked) instance.save() return instance
def update(self, instance, validated_data): instance.name = validated_data.get('name', instance.name) instance.description = validated_data.get('description', instance.description) instance.photo = validated_data.get('photo', instance.photo) instance.is_public = validated_data.get('is_public', instance.is_public) instance.author = validated_data.get('author', instance.author) instance.tags = validated_data.get('tags', instance.tags) instance.favorite_by = validated_data.get('favorite_by', instance.favorite_by) instance.servings = validated_data.get('servings', instance.servings) instance.difficulty = validated_data.get('difficulty', instance.difficulty) instance.ratings = validated_data.get('ratings', instance.ratings) instance.save() if 'preparation_steps' in validated_data: # Delete any preparation steps not included in the request. preparation_step_ids = [item.get('id') for item in validated_data['preparation_steps']] for preparation_step in instance.preparation_steps.all(): if preparation_step.id not in preparation_step_ids: preparation_step.delete() # Create or update preparation step instances that are in the request. for item in validated_data['preparation_steps']: preparation_step = PreparationStep(id=item.get('id'), text=item['text'], step_number=item['step_number'], recipe=instance) preparation_step.save() if 'ingredients' in validated_data: # Delete any quantified ingredients not included in the request. quantified_ingredient_ids = [item.get('id') for item in validated_data['ingredients']] for quantified_ingredient in instance.ingredients.all(): if quantified_ingredient.id not in quantified_ingredient_ids: quantified_ingredient.delete() # Create or update quantified ingredient instances that are in the request. for item in validated_data['ingredients']: units = Unit(**item['units']) units.save() name = Ingredient(**item['name']) name.save() quantified_ingredient = QuantifiedIngredient(id=item.get('id'), qty=item['qty'], name=name, units=units, recipe=instance, original_recipe=instance) quantified_ingredient.save() return instance
def ingredients(): Ingredient.objects.all().delete() for _ in range(amount_ingredients): ingred = Ingredient(name=rword(1, "")) ingred.save()