To achieve the functionality where you can see how many students have selected food1 and how many have selected food2, you need to create a relation between the DayOfWeek and the students' selection of a specific lunch. You can create an additional model to capture each student's food selection on a particular day.
Here's an overview of what you need to add:
Create a new model to track student food selection.
Link the new model to the DayOfWeek and Lunch models, as well as to the student.
Updated Models:
Add Student Food Selection Model: This new model will track each student's food choice for a given day.
from odoo import models, fields
class Student(models.Model):
_name = 'food.student'
_description = 'Student'
name = fields.Char(string='Student Name', required=True)
student_id = fields.Char(string='Student ID', required=True)
class Lunch(models.Model):
_name = "food.foods"
name = fields.Char(string="Food Name", required=True)
food_type = fields.Selection([('withrice', 'WithRice'), ('whitoutrice', 'WithoutRice')], string="Select")
class DayOfWeek(models.Model):
_name = 'food.days'
_description = 'Days of the Week'
name = fields.Char(string='Day', compute='_compute_name')
date = fields.Date(string='Date', required=True)
week_id = fields.Many2one('food.weeks', string='Week')
food_ids = fields.Many2many('food.foods', string='Food Name')
class StudentFoodSelection(models.Model):
_name = 'food.student.selection'
_description = 'Student Food Selection'
student_id = fields.Many2one('food.student', string='Student', required=True)
day_id = fields.Many2one('food.days', string='Day', required=True)
food_id = fields.Many2one('food.foods', string='Selected Food', required=True)
Explanation:
Student model:
Added a Student model to represent each student. You might already have a similar student model, in which case, you can skip this one.
Lunch model (food.foods):
This is your existing Lunch model. No changes here.
DayOfWeek model (food.days):
This is also your existing model for representing each day. The only change is a change in the field name from food_id to food_ids to make it clear it's a list of options.
StudentFoodSelection model (food.student.selection):
This new model captures each student's food choice on a given day. It has three fields:
student_id: A reference to the student.
day_id: A reference to the day for which the selection is made.
food_id: A reference to the food selected by the student
Computing the Selection Count
To calculate how many students have selected food1 or food2, you can add a computed field on the DayOfWeek model or create a method to run the calculation:
class DayOfWeek(models.Model):
_inherit = 'food.days'
food_selection_summary = fields.Text(string='Food Selection Summary', compute='_compute_food_selection_summary')
def _compute_food_selection_summary(self):
for record in self:
summary = ""
food_counts = {}
selections = self.env['food.student.selection'].search([('day_id', '=', record.id)])
for selection in selections:
if selection.food_id.name not in food_counts:
food_counts[selection.food_id.name] = 0
food_counts[selection.food_id.name] += 1
summary = "\n".join(f"{food_name}: {count} students" for food_name, count in food_counts.items())
record.food_selection_summary = summary