Login and get codingIn order to measure daily food intake as a user our current
Food
model is not enough. We also needUser
andFoodEntry
models, so let's create them!In database-speak,
FoodEntry
is a many-to-many relation betweenUser
andFood
. Users would add many foods and foods will be added by many users.For this learning path, we'll work with these Pydantic models in memory to keep things simpler. In the SQLModel learning path we'll use a database.
Create the
User
andFoodEntry
models based on what you learned in the second FastAPI Bite in this series.Required attributes and their types:
User
-
id
-> int
-username
->str
-password
->str
(needed for authentication later)* For
password
override the constructor (__init__.py
) to hash the password upon creation of the module. You can use the providedget_password_hash()
function for this.FoodEntry
-
id
->int
(what in a DB would be the primary key)
-user
->User
(here we see that Pydantic models can be nested)
-food
->Food
(from previous Bite and provided in the template)
-date_added
->datetime
(defaults todatetime.now()
)
-number_servings
->float
(fitness nerds can be quite exact about this)* Additionally add a property to calculate the total calories of a food entry (
number_servings * food.kcal_per_serving
) and call ittotal_calories
.Good luck and in the next Bite we'll expand our API to work with these new objects.
97 out of 101 users completed this Bite.
Will you be Pythonista #98 to crack this Bite?
Resolution time: ~58 min. (avg. submissions of 5-240 min.)
Pythonistas rate this Bite 5.0 on a 1-10 difficulty scale.
» Up for a challenge? 💪