Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

Get model data in a controller

By
Tobias Wetzel
on 4/1/15, 8:38 AM 2,625 views

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!

-1
Tobias Wetzel
On 7/18/15, 4:15 AM

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) 



1

Axel Mendoza

--Axel Mendoza--
10205
| 7 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

Axel Mendoza
On 7/17/15, 12:24 PM

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

0
eLBati
On 7/19/15, 11:32 AM

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

0

Emipro Technologies Pvt. Ltd.

--Emipro Technologies Pvt. Ltd.--
Tiny ERP Pvt Ltd
http://www.odoo.com
5975
| 6 7 8
Rajkot, India
--Emipro Technologies Pvt. Ltd.--

Being an emerging leader in IT market since 2011, Emipro Technologies Pvt. Ltd. has been providing a wide range of business solutions in Odoo & Magento. We are pleased to have a large pool of contented customers with our meticulous work in the domain of ERP & e-Commerce. A ray of relief & satisfaction to  our customers heart by our successful deployment in their organization, purely represents our success in Odoo platform. Hence, we take pride for being an Odoo partner with a vision of expanding our strategic alliances with our customers to offer them high value-added, trustworthy & cost effective solutions.

 

Since establishment, our business has span across 11 countries of the world. Our customers are companies of all sizes ranging from start-ups to large enterprises, who realize that they need a professional business solutions to generate revenue streams, establish proper communication channels or streamline business operations. Standing with strong determination of customer satisfaction, observing each minute detail of their business processes, providing proper guidance and moving forth to develop product accordingly adds our value and reliability among our customers. 

 

We are blessed with efficient, passionate & eclectic young developers who have come across almost all kinds of business profiles, working with full dedication, applying creativity & new features in existing modules and completing customer's projects successfully on predefined target. Our in-depth knowledge while giving business solutions in Odoo allows us to offer following services to our customers :

 

  • Consulting
  • Installation, Configuration & Customization
  • Training & Support
  • Maintenance
  • Upgradation
  • Documentation
  • Crafting community modules
  • Drafting videos on demand
  • Smart Apps development

We will be :) to welcome you @ www.emiprotechnologies.com to solve any kind of your business needs around Odoo. However we are just an inch away from you by email info@emiprotechnologies.com or a tweet to @EmiproTech

Emipro Technologies Pvt. Ltd.
On 4/1/15, 9:24 AM

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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 4/1/15, 8:38 AM
Seen: 2625 times
Last updated: 8/14/15, 12:50 PM