leaft: Day 3, Sept 16th
Daily serving summation
With finishing up my last days at General Assembly, I’ve been so busy I haven’t documented my leaft entirely!
At this point, leaft is almost done. I still need to add a signup feature, but other than that, I am happy with the UI and the functionality.
When I last left off, I was still building out routes in my backend and creating relationships between my two models. I originally decided to have DailyConsumption be my parent class and WeeklyConsumption
be my child class, however, it made more sense to have WeeklyConsumption
be the parent, WeeklyConsumption
can have a one-to-many relationship with DailyConsumption (i.e. one week can have many days).
DailyConsumption
has a field called weekly_consumption
, which will reference the WeeklyConsumption
model and summate all the servings (obtained from the daily_servings
field of the DailyConsumption
model) for a particular week. I specifically want to breakdown the consumption_sum
method I created to summate the servings:
def consumption_sum(self):
days = self.dailyconsumption_set.all()
counter = 0
for day in days:
counter += day.daily_servings
return counter
weekly_total = property(fget=consumption_sum)
Let’s break it down further:
def consumption_sum(self):
days = self.dailyconsumption_set.all()
The method def consumption_sum
uses the self parameter, which is used to represent an instance (object) of the given class, meaning every time a new week is created with the WeeklyConsumption class, it is referring to the new instance of the object.
I created a variable called days
which is equal to dailyconsumption_set.all()
. This is all the DailyConsumption
models that are related to an instance of the WeeklyConsumption
class (remember, DailyConsumption
is related to WeeklyConsumption
by a foreign key).
The next step is the for-in loop:
counter = 0
for day in days:
counter += day.daily_servings
return counter
The loop counts for each instance (class) of DailyConsumption
(meaning, each daily consumption log) in self.dailyconsumption_set.all()
related to one instance of WeeklyConsumptio
n by the foreign key value. .all()
means to take all the daily consumption logs related to a week. Then for each instance of DailyConsumption
, dot notation is used to access daily_servings
, which is the number of servings the user consumed in one specific day. It adds the daily servings to the counter, and I returned the counter.
To get a clearer picture of what this looks like, here is what the get request looks like in Postman for the endpoint:
https://backendproject4.herokuapp.com/meat_consumption/weekly_consumption/3/daily_consumption.
The URL uses my deployed backend URL and grabs all the daily consumptions for the 3rd weekly consumption in the database. Here is the result:
[{"id": 3,"weekly_consumption_id": 3,"owner": "narissa5","consumed": "No","daily_servings": 0,"day_consumed": "2020-09-21T22:18:46.868Z","weekly_total": 2},{"id": 5,"weekly_consumption_id": 3,"owner": "narissa5","consumed": "No","daily_servings": 0,"day_consumed": "2020-09-22T04:00:00.000Z","weekly_total": 2},{"id": 6,"weekly_consumption_id": 3,"owner": "narissa5","consumed": "Yes","daily_servings": 2,"day_consumed": "2020-09-23T04:00:00.000Z","weekly_total": 2}]
And that’s how I used a for-in loop to summate the daily_servings
! At this point, my routes are all in place and I’m ready to move onto my frontend. I will by using Vue CLI to fetch data from my backend. The next post will detail the troubles I faced there…