Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
4 ตอบกลับ
37666 มุมมอง

Hi everyone,

I am feeling kind of stupid, but nevertheless here is my problem:

I defined a model class:

models.py

class MealType(models.Model):
_name = 'event_participation.meal_type'

name = fields.Char(string="Name", required=True)
event_id = fields.Many2one('event.event', string="Event")
cost = fields.Float(string="Additional Cost")

def get_all_meal_types(self):
return self.search([])

Now I have a controller in which I want to get all MealType records from the database to display them.

controllers.py

import models

class ExtendedSaleController(main.website_sale):

def checkout_values(self, data=None):
if not data:
cr, uid, context, registry = request.cr, request.uid, request.context, request.registry
values = super(ExtendedSaleController, self).checkout_values()


#retrieve meal types

values["meals"] = meals

      return values

I tried several things to get the entries but nothing worked:

meal = models.MealType()
meals = models.MealType.get_all_meal_types(self)
meals = models.MealType.search([])
meals = meal.search([])  

 What am I missing?

Thanks for your help!

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

From within a controller, you can access ORM models using http.request.env

In your case:

MealType = http.request.env['event_participation.meal_type']
mealtypes = MealType.search([])

See the website tutorial

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

The thing is that in controllers you access the models and their methods using the registry like self.pool in the old api.

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hi,

you can manage those data as like below into your controller.


def checkout_values(self, data=None):

if not data:

cr, uid, context, registry = request.cr, request.uid, request.context, request.registry

values = super(ExtendedSaleController, self).checkout_values()

#retrieve meal types

values["meals"] =registry['event_participation.meal_type'].search([])

return values

อวตาร
ละทิ้ง
ผู้เขียน คำตอบที่ดีที่สุด

Solution:

orm_meals = registry.get('event_participation.meal_type') 

get the IDs of all MealType records:

meal_ids = orm_meals.search(cr, SUPERUSER_ID, [], context=context) 

and get the complete records as objects:

meals = orm_meals.browse(cr, SUPERUSER_ID, meal_ids, context) 



อวตาร
ละทิ้ง

should I declare 'registry'? if, yes, how? , i'll appreciate your answer, thanks in advance. I have the same problem in the above question

Related Posts ตอบกลับ มุมมอง กิจกรรม
2
ก.ค. 24
2634
0
ก.ย. 19
3496
1
ต.ค. 20
12598
1
มิ.ย. 15
18241
1
มี.ค. 15
5066