Pular para o conteúdo
Menu
Esta pergunta foi sinalizada
2 Respostas
1182 Visualizações

I making a module for student's to select their lunch in a day and it's a selection .
how i can see how many student's select food1 and how many select food2 ?


this is my food class :

class Lunch(models.Model):
_name = "food.foods"

name = fields.Char(string="نام غذا", required=True)
food_type = fields.Selection([('withrice','WithRice'),('whitoutrice' , 'WithoutRice')] , string="select" )

and this is my days class :

class DayOfWeek(models.Model):
_name = 'food.days'
_description = 'days of week'

name = fields.Char(string='day' , compute='_complite_name')
date = fields.Date(string='date', required=True)
week_id = fields.Many2one('food.weeks', string='week')
food_id = fields.Many2many('food.foods', string='food name')






Avatar
Cancelar
Melhor resposta

Hello mmd,


To see how many students selected Food 1 and Food 2, you can add two counters in your "food.days" model.

Based on your Many2many relationship with food_ids, you can count the selections as follows:


Add compute fields:

//Code in Comment//

Hope this Helps,

If you need any help in customization feel free to contact us.


Thanks & Regards,

Email:  odoo@aktivsoftware.com           

Skype: kalpeshmaheshwari

Avatar
Cancelar

Code :
with_rice_count = fields.Integer(string='With Rice Count',
compute='_compute_food_counts',
store=True)

without_rice_count = fields.Integer(string='Without Rice Count',
compute='_compute_food_counts',
store=True)

Compute method:
@api.depends('food_id')
def _compute_food_counts(self):
for record in self:
with_rice = record.food_id.filtered(
lambda f: f.food_type == 'withrice')
without_rice = record.food_id.filtered(
lambda f: f.food_type == 'whitoutrice')
record.with_rice_count = len(with_rice)
record.without_rice_count = len(without_rice)

Melhor resposta

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





Avatar
Cancelar
Publicações relacionadas Respostas Visualizações Atividade
0
jul. 21
2514
3
dez. 19
4184
0
abr. 22
3987
1
mai. 21
3738
3
mar. 24
6882