Skip to Content
Odoo Menu
  • Sign in
  • 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
  • Accounting
  • Inventory
  • PoS
  • Project
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
Help

how to get unique list of dictionaries ?

Subscribe

Get notified when there's activity on this post

This question has been flagged
poslistloopdictionaryarray
3 Replies
7607 Views
Avatar
Mohamed Fouad (personal)

the dictionary on itself it has unique keys, but if I'm appending the dictionaries into a list, every dictionary is being separated with its unique values and it may be duplicated keys, every key in the separate dictionary into the list,

in my case  I'm working product summary report for POS orders, I make a method that loop over all pos order lines and retrieve data from there, the final result should be unique product name with the sum of sold quantities to this product overall orders, I did it, but the output returns non-unique products and the length of the list is the summation of the order lines, I need  to remove all duplicated product and set only the last one which has the maximum quantity, here is my code 

@api.multi
    def product_summary_test(self):
        product_summary_dict = {}
        data = []
        if self.date_from and self.date_to:
            order_detail = self.env['pos.order'].search([('date_order', '>=', self.date_from),
                                                         ('date_order', '<=', self.date_to)])
            if order_detail:
                for each_order in order_detail:
                    for each_order_line in each_order.lines:
                        if each_order_line.product_id.name in product_summary_dict:
                            product_qty = product_summary_dict[each_order_line.product_id.name]
                            product_qty += each_order_line.qty
                            res1 = {
                                "name": each_order_line.product_id.name,
                                "sold_qty": product_qty,
                            }
                            data.append(res1)
                        else:
                            product_qty = each_order_line.qty
                            res2 = {
                                "name": each_order_line.product_id.name,
                                "sold_qty": product_qty,
                            }
                            data.append(res2)
                        product_summary_dict[each_order_line.product_id.name] = product_qty;
        if data:
            print(len(data))
            print(data)
            return data
        else:
            return {}
the output be like

[{'name': 'x', 'sold_qty': 2.0}, {'name': 'x', 'sold_qty': 8.0},{'name': 'x', 'sold_qty': 12.0}, {'name': 'y', 'sold_qty': 5.0}, {'name': 'y', 'sold_qty': 7.0, {'name': 'y', 'sold_qty': 9.0}]

its overwrite the same product and add the new quantity in sold_qty 2 + 6 + 2 

it should be just :

[{'name': 'x', 'sold_qty': 12.0},{'name': 'y', 'sold_qty': 9.0}]
how that can be done 

thanks in advance

 
1
Avatar
Discard
Avatar
Axel Mendoza
Best Answer

Hi @Mohamed Fouad

Try it like this:

    @api.multi
    def product_summary_test(self):
        product_summary_dict = {}
        if self.date_from and self.date_to:
            order_detail = self.env['pos.order'].search([
                ('date_order', '>=', self.date_from),
                ('date_order', '<=', self.date_to)
            ])
            if order_detail:
                for each_order in order_detail:
                    for each_order_line in each_order.lines:
                        product_summary_dict[each_order_line.product_id.name] = product_summary_dict.get(each_order_line.product_id.name, 0.0) + each_order_line.qty
        data = [{"name": key, "sold_qty": value} for (key, value) in product_summary_dict.items()]
        if data:
            print(len(data))
            print(data)
            return data
        else:
            return {}

Hope it help you

0
Avatar
Discard
Avatar
Paresh Wagh
Best Answer

Hi Mohamed:

You can use the dict itself to keep a track of the summary information like so. The update and get allow you to handle situations with missing keys in the dict gracefully.

product_summary_dict.update({each_order_line.product_id.name: {
"name": each_order_line.product_id.name,
"sold_qty": product_qty + product_summary_dict.get(each_order_line.product_id.name, {}).get("sold_qty", 0.0)
}})


0
Avatar
Discard
Mohamed Fouad (personal)
Author

i think it may be suitable, but can you explain it more or merge it in my code and paste it again

thanks dear

Enjoying the discussion? Don't just read, join in!

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

Sign up
Related Posts Replies Views Activity
how i can update list in Directory odoo 12 Solved
list dictionary
Avatar
Avatar
Avatar
2
Aug 19
5405
Why do the product variants not appear at the points of sale in the form of a list
pos list odoo
Avatar
0
May 24
1249
How append the values in one2many field in odoo10? Solved
one2many list dictionary odoo10
Avatar
Avatar
2
Nov 21
33998
how to overwrite duplicated keys ?
pos summary array odoo12
Avatar
Avatar
1
Jul 20
3959
How to insert list value in dictionary for default_get method Odoo11 Solved
list dictionary odoo odoo10
Avatar
Avatar
1
Oct 18
10894
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