Python – Odoo 10 goes from custom models to purchase lists

Odoo 10 goes from custom models to purchase lists… here is a solution to the problem.

Odoo 10 goes from custom models to purchase lists

I pull data from external sources like this :

from odoo import models,fields,api
import datetime
import requests
import logging
_logger = logging.getLogger(__name__)
class purchase_order(models. Model):

_inherit = "purchase.order"

@api.model
def getOrdersTechData(self):

getServer = 'someapi.xxx'

get_response = requests.get(url=getServer).json()
     partner_id = get_response['partner_id']
     name = get_response['name']
     product_id = get_response['product_id']
     ...

self.createBestelAanvraag(partner_id,name,product_id,product_qty,product_uom,price_unit,date_planned)

@api.multi
def createBestelAanvraag(self,partner_id,name,product_id,product_qty,product_uom,price_unit,date_planned):
     _logger.debug("name:")
     _logger.debug(name)
     self. PurchaseOrder = self.env['purchase.order']
     po_vals = {
        'partner_id': partner_id,
        'order_line': [
            (0, 0, {
                'name': name,
                ...
            }),
           ],
    }

self.po = self. PurchaseOrder.create(po_vals)

I launch it from the menu item on the home screen as shown below:

<pre class=”lang-xml prettyprint-override”><?xml version="1.0" encoding="utf-8"?>

<odoo>
<data>
<record id="action_make_testing" model="ir.actions.server">
<field name="name">My Action</field>
<field name="model_id" ref="model_purchase_order"/>
<field name="code">env['purchase.order'].getOrdersTechData()
</field>
</record>

<menuitem name="Fetch Data" action="action_make_testing"
id="sale_order_custom_document" sequence="20"/>
</data>
</odoo>

But after creating the order, I see a blank View and I have to make a purchase from the GUI. Instead, I want to immediately see a Purchasing ListView that contains all orders, including new orders.

Solution

You can return repo buy operation.

Try this :

 action = self.env.ref('purchase.purchase_rfq').read()[0]
 action.update({'domain': [('id', '=', self.po.id)], 'res_id': self.po.id})
 return action

This opens the Purchase View and displays the purchase order.

Related Problems and Solutions