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.

0

DISPLAYING THE SAME ORDER OF KEY WHICH HAVE IN MY LIST OF DICTIONARY KEYS IN MY OUTPUT

By
Dasadiya Chaitanya
on 3/27/15, 2:10 AM 601 views

I am using python 2.7.X version and I need to display my result of list of dictionary in key order same as in my output


CREATE THE LIST OF DICTIONARY : 

 

for acc_data in acc_pool.browse(cr,uid,acc_ids_in):

for line in acc_data.invoice_line:

c+=1

lst_data2.append({

'SupplierName':acc_data.partner_id.name or '',

'SupplierBRN':acc_data.partner_id.com_reg_no1 or '',

'InvoiceDate':acc_data.date_invoice or '',

'InvoiceNumber':acc_data.number or '',

'ImportDeclarationNo':'',

'LineNumber':c,

'ProductDescription':line.product_id.name or '',

'PurchaseValueMYR':line.price_unit or 0.00,

'GSTValueMYR':'',

'TaxCode':line.invoice_line_tax_id.name or '',

'FCYCode':'',

'PurchaseFCY':'',

'GSTFCY':'',

})


RESULT ::

my result displaying from the ubuntu terminal

lst_data2 ==========>>>lst_data2 [{'ProductDescription': u'Ink Cartridge', 'SupplierBRN': '', 'ImportDeclarationNo': '', 'GSTValueMYR': '', 'SupplierName': u'Vicking Direct', 'GSTFCY': '', 'TaxCode': u'Purchase Tax 15.00%', 'InvoiceDate': '2015-03-24', 'FCYCode': '', 'PurchaseFCY': '', 'PurchaseValueMYR': 58.0, 'LineNumber': 1, 'InvoiceNumber': u'EXJ/2015/002'}, {'ProductDescription': u'Toner Cartridge', 'SupplierBRN': '', 'ImportDeclarationNo': '', 'GSTValueMYR': '', 'SupplierName': u'Vicking Direct', 'GSTFCY': '', 'TaxCode': u'OTAX X', 'InvoiceDate': '2015-03-24', 'FCYCode': '', 'PurchaseFCY': '', 'PurchaseValueMYR': 65.0, 'LineNumber': 2, 'InvoiceNumber': u'EXJ/2015/002'}]

hear u can easily see my key of order is different from in my result

My Question is that i need to set the all the keys from same as created in list of dictionary above from the list.

How can I set the same order of list of keys from in my result.??





use ordered dict. instead of regular dict. , visit this link to know use of order dict, http://pymotw.com/2/collections/ordereddict.html

Mansi Kariya (mka)
on 3/27/15, 4:50 AM

@Mansi Kariya I have resolved that one from my side Be a Coooool ..... :) Thanks For Your Comment

Dasadiya Chaitanya
on 3/27/15, 6:53 AM
0
Dasadiya Chaitanya
On 3/27/15, 7:02 AM

Solution from the Above Question...

Just need to import the Following Python  Library for the code

from collections import OrderedDict

for acc_data in acc_pool.browse(cr,uid,acc_ids_in):

c=0

      for line in acc_data.invoice_line:

          if line.invoice_line_tax_id:

              c+=1

              lst_data2.append(OrderedDict([

                  ('SupplierName',acc_data.partner_id.name or ''),

                  ('InvoiceNumber',acc_data.number or ''),

                  ('ImportDeclarationNo',''),

                  ('LineNumber',c),

                  ('ProductDescription',line.product_id.name or ''),

                  ('PurchaseValueMYR',line.price_unit or 0.00),

                  ('GSTValueMYR',''),

                  ('TaxCode',line.invoice_line_tax_id.name or ''),

                  ('FCYCode',''),

                  ('PurchaseFCY',''),

                  ('GSTFCY',''),

                  ]))

OrderedDict is a dictionary subclass that remembers the order in which its contents are added.

I my case the OrderedDict is the part of the python Collection Type Which is used to set the Order of the keys from the dictionary.


0

deep

--deep--
1617
| 5 4 7
Bengaluru, India
--deep--

Techno-Functional Associate with 6+ years of experience in Odoo (formerly known as OpenERP).

deep
On 3/27/15, 7:17 AM

A built in function "itemgetter" exists to sort/order a Dictionary by specifying its item-key

Example:

list_to_be_sorted = [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}]

from operator import itemgetter

newlist = sorted(list_to_be_sorted, key=itemgetter('name'))

@deep I doen't want to sort the keys of the dictionary I resolved that one from my side see below answer Thanks for your reply

Dasadiya Chaitanya
on 3/27/15, 7:42 AM

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

1 follower(s)

Stats

Asked: 3/27/15, 2:10 AM
Seen: 601 times
Last updated: 5/9/16, 12:50 AM