# meal_tracker/models.py from django.db import models class Ingredient(models.Model): name = models.CharField(max_length=255, db_index=True) calories_per_100g = models.FloatField() protein_per_100g = models.FloatField() fat_per_100g = models.FloatField() carbs_per_100g = models.FloatField() class Meta: verbose_name_plural = "Ingredients" def __str__(self): return self.name class Food(models.Model): name = models.CharField(max_length=255, unique=True) estimated_calories = models.IntegerField(null=True, blank=True) estimated_protein = models.FloatField(null=True, blank=True) estimated_fat = models.FloatField(null=True, blank=True) estimated_carbs = models.FloatField(null=True, blank=True) ingredients = models.ManyToManyField('Ingredient', through='FoodComponent') @property def total_calories(self): if self.estimated_calories: return self.estimated_calories return sum(c.get_component_calories() for c in self.ingredients.all()) def __str__(self): return self.name class FoodComponent(models.Model): food = models.ForeignKey(Food, related_name='components', on_delete=models.CASCADE) ingredient = models.ForeignKey(Ingredient, related_name='foods_used_in', on_delete=models.CASCADE) amount_grams = models.FloatField() calories = models.IntegerField(default=0) protein = models.FloatField(default=0) fat = models.FloatField(default=0) carbs = models.FloatField(default=0) def save(self, *args, **kwargs): if self.ingredient: factor = self.amount_grams / 100.0 self.calories = int(self.ingredient.calories_per_100g * factor) self.protein = self.ingredient.protein_per_100g * factor self.fat = self.ingredient.fat_per_100g * factor self.carbs = self.ingredient.carbs_per_100g * factor super().save(*args, **kwargs) def __str__(self): return f"{self.food.name} component" class Meal(models.Model): date = models.DateField() created_at = models.DateTimeField(auto_now_add=True) def get_total_macros(self): totals = {'calories': 0, 'protein': 0, 'fat': 0, 'carbs': 0} for item in self.items.all(): totals['calories'] += item.get_food_calories() totals['protein'] += item.get_food_protein() totals['fat'] += item.get_food_fat() totals['carbs'] += item.get_food_carbs() return totals def __str__(self): return f"Meal on {self.date}" class MealItem(models.Model): meal = models.ForeignKey(Meal, related_name='items', on_delete=models.CASCADE) food = models.ForeignKey(Food, related_name='meals_contained', on_delete=models.PROTECT) quantity_grams = models.FloatField() def get_food_calories(self): if self.food.estimated_calories: return int(self.food.estimated_calories) total = 0 for comp in self.food.components.all(): factor = self.quantity_grams / comp.amount_grams total += int(comp.calories * factor) return total def get_food_protein(self): if self.food.estimated_protein is not None: return self.food.estimated_protein total = 0 for comp in self.food.components.all(): factor = self.quantity_grams / comp.amount_grams total += comp.protein * factor return total def get_food_fat(self): if self.food.estimated_fat is not None: return self.food.estimated_fat total = 0 for comp in self.food.components.all(): factor = self.quantity_grams / comp.amount_grams total += comp.fat * factor return total def get_food_carbs(self): if self.food.estimated_carbs is not None: return self.food.estimated_carbs total = 0 for comp in self.food.components.all(): factor = self.quantity_grams / comp.amount_grams total += comp.carbs * factor return total def __str__(self): return f"{self.food.name} ({self.quantity_grams}g)"