Skip to Content
Odoo Menu
  • Prijavi
  • Try it free
  • Apps
    Finance
    • Accounting
    • Invoicing
    • Expenses
    • Spreadsheet (BI)
    • Documents
    • Sign
    Sales
    • CRM
    • Sales
    • POS Shop
    • POS Restaurant
    • Subscriptions
    • Rental
    Websites
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventory
    • Manufacturing
    • PLM
    • Purchase
    • Maintenance
    • Quality
    Human Resources
    • Employees
    • Recruitment
    • Time Off
    • Appraisals
    • Referrals
    • Fleet
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Events
    • Marketing Automation
    • Surveys
    Services
    • Project
    • Timesheets
    • Field Service
    • Helpdesk
    • Planning
    • Appointments
    Productivity
    • Discuss
    • Approvals
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage Distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Estate Management
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Manufacturing
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Community
    Learn
    • Tutorials
    • Documentation
    • Certifications
    • Training
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Download
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Events
    • Translations
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Pricing
  • Help

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Knjigovodstvo
  • Zaloga
  • PoS
  • Projekt
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Ključne besede (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Ključne besede (View all)
odoo accounting v14 pos v15
About this forum
Pomoč

order by selection field in List view

Naroči se

Get notified when there's activity on this post

This question has been flagged
2 Odgovori
23770 Prikazi
Avatar
Prakash

In openerp 7 list view i want to shows the state value sort in the order draft, assigned and cancel currently shows in Asc or Desc. But in my case we need sorting in the order draft, assigned and cancel state. Based on applied in order by in python file

For example in the SQL the code:-

       select state, date from  object_name
ORDER BY CASE WHEN state = 'draft'  THEN 0 
              WHEN state = 'assigned'  THEN 1 
              WHEN state = 'cancel'  THEN 2
              ELSE 3
         END,  date desc

The above sql code applied in the python

_order = "CASE WHEN state='draft'  THEN 0   WHEN state = 'assigned'  THEN 1 ELSE 2 END,  date desc"

In the above query sorting selection value working in the pg_admin but in the python code its shows below error

Invalid "order" specified. A valid "order" specification is a comma-separated list of valid field names (optionally followed by asc/desc for the direction)

Based on this sorting order by selection value how to apply in openerp ? Override search method also applied the same sql query but shows same issue

2
Avatar
Opusti
Torsten Francke

it is maybe helpfull to see the complete sql statment which was produce by openerp

Prakash
Avtor

Overridden search method and applied the sql statement same issue please let me provide more details to how to fix issues ? thanks

Avatar
Torsten Francke
Best Answer

per default you can only sort by fields but you can override this

the following code:

    def _generate_order_by(self, order_spec, query):
        my_order = "CASE WHEN state='draft'  THEN 0   WHEN state = 'assigned'  THEN 1 ELSE 2 END,  date desc"            
        if order_spec:
            return super(sale_order, self)._generate_order_by(order_spec, query) + ", " + my_order
        return " order by " + my_order

to your sales_order model. I think this solves your problem. order_spec is given by the client or other order statment and add your stuff as well.

Hint: Overriding this method can add security holes like sql-injection if you do not check carefully your code and know what you do

UPDATE: I build a more generic version to update the sorting of 'state' as defined in the model instead of the name. I publish in our blog: http://blog.initos.com/2013/08/15/openerp-sortieren-nach-prozess-status/ with a german problem description but you can see the code

6
Avatar
Opusti
Prakash
Avtor

thanks for reply i will try

Prakash
Avtor

In the above code works well in the number of records. but in the empty record shows below issue File "C:\Program Files (x86)\OpenERP 7.0-20130308-002138\Server\server.\openerp\sql_db.py", line 226, in execute ProgrammingError: syntax error at or near "by" LINE 1: ...".id FROM "object.name" WHERE FALSEorder by CASE WH... please guide me how to fix ?

Torsten Francke

add a space in 'return " order by " + my_order' before 'order by', i updated the code above

Prakash
Avtor

Thanks solved

Prakash
Avtor

Hi Markus, The above code worked for default view i also want the same functionality In the List view Header State Field clicked by order by ASC and order by DESC (based on the above _generate_order_by query). I think need to search method override is any sample code available based on this. Thanks

Avatar
Ethan Furman
Best Answer

A version of Markus' answer from his blog. This one only changes the state ordering if state was in the query.

def _generate_order_by(self, order_spec, query):
    "correctly orders state field if state is in query"
    order_by = super(this_table_class, self)._generate_order_by(order_spec, query)
    if order_spec and 'state ' in order_spec:
        state_column = self._columns['state']
        state_order = 'CASE '
        for i, state in enumerate(state_column.selection):
            state_order += "WHEN %s.state='%s' THEN %i " % (self._table, state[0], i)
        state_order += 'END '
        order_by = order_by.replace('"%s"."state" ' % self._table, state_order)
    return order_by
2
Avatar
Opusti
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Prijavi
Community
  • Tutorials
  • Documentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Translations
Services
  • Odoo.sh Hosting
  • Support
  • Upgrade
  • Custom Developments
  • Education
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Brand Assets
  • Contact us
  • Jobs
  • Events
  • Podcast
  • Blog
  • Customers
  • Legal • Privacy
  • Security
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now