自定义模块在Openerp中自动添加客户

时间:2014-02-27 12:18:50

标签: openerp openerp-7

我有一个自定义模块,我继承了res.partner。我有一个字段“医生”(这是很多2字段)。当我创建一个客户时,我可以选择一个医生。在img 1 Willam是客户,他已经选择了Nitesh作为医生。我已经创建了doctor_view.xml,它将在img 2中显示Doctor.Now的所有细节,因为我们选择“Nitesh”为'Customer Willam'的'Doctor',我应该显示“Willam在img2中的“客户”下。任何人都可以帮助我吗?在此先感谢 Img1 Img2

我的代码

Customer.py

from qrcode import *
from osv import osv
from osv import fields

class res_partner(osv.osv):
 _inherit = "res.partner"
 _description = "adding fields to res.partner"
 _columns = { 

    'doctor': fields.many2one('crm.lead.doctor','Doctor'),
 }
 class crm_lead_doctor(osv.osv):
    _name = "crm.lead.doctor"
    _order = "name"
    _columns ={
        'name':fields.char('Doctor Name',required=True,size=64,translate=True),
        'doctor_id':fields.char('Doctor Id',size=64,readonly=True),
        'doctor_mobile': fields.char('Mobile',required=True,size=64),
        'doctor_email': fields.char('Email',size=64),
        'doctor_hospital': fields.many2one('crm.lead.hospital','Hospital'),
        'doctor_street': fields.char('Street', size=128),
        'doctor_street2': fields.char('Street2', size=128),
        'doctor_zip': fields.char('Zip', change_default=True, size=24),
        'doctor_city': fields.char('City', size=128),
        'doctor_state_id': fields.many2one("res.country.state", 'State'),
        'doctor_country_id': fields.many2one('res.country', 'Country'),
        'doctor_brochure': fields.char('Brochuer',size=64),
        'doctor_flyer': fields.char('Flyer',size=64),
        'doctor_training': fields.char('Training',size=64),
        'doctor_starterpacksent': fields.char('Strater pack sent',size=64),
        'doctor_no_of_deliveries': fields.char('No of Deliveries/year',size=64),
        'doctor_fee': fields.char('Fee',size=64),
        'doctor_registration': fields.char('Registration No',size=64),
        'doctor_pancard': fields.char('Pan Card No',size=64),
        'doctor_fiscalcode': fields.char('Fiscal Code',size=64),
        'doctor_iban': fields.char('IBAN',size=64),
        'doctor_contractset': fields.char('Contract Set',size=64),
        'doctor_contractrecieved': fields.char('Contract Recieved',size=64),
        'doctor_clients':fields.many2many('res.partner')
    }
    def create(self, cr, uid, vals, context={}):
        doc_seq = self.pool.get('ir.sequence').get(cr, uid, 'master.doctor')
        vals['doctor_id'] = doc_seq
        res = super(res_partner.crm_lead_doctor, self).create(cr, uid, vals, context)
        return res 

Doctor_view.xml

<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>

    <record id="view_doctor_form_extended" model="ir.ui.view">
    <field name="name">crm.lead.doctor.form</field>
    <field name="model">crm.lead.doctor</field>
    <field name="arch" type="xml">
        <form string="Doctor Details" version="7.0">
            <group>
                <field name="name"/>
<!--                 <field name="doctor_id"/> -->
                <field name="doctor_mobile"/>
                <field name="doctor_email"/>
                <field name="doctor_hospital"/>
                <label for="street" string="Doctor Address"/>
                <div>
                    <field name="doctor_street" placeholder="Street..."/>
                    <field name="doctor_street2"/>
                    <div class="address_format">
                        <field name="doctor_city" placeholder="City" style="width: 40%%"/>
                        <field name="doctor_state_id" on_change="onchange_state(state_id)" options='{"no_open": True}' placeholder="State" style="width: 24%%"/>
                        <field name="doctor_zip" placeholder="ZIP" style="width: 34%%"/>
                    </div>
                    <field name="doctor_country_id" placeholder="Country" options='{"no_open": True}'/>
                </div>
            </group>
            <notebook>
                <page string="Sales">
                    <group>
                        <group>
                            <field name="doctor_brochure"/>
                            <field name="doctor_flyer"/>
                            <field name="doctor_training"/>
                            <field name="doctor_starterpacksent"/>
                            <field name="doctor_no_of_deliveries"/>
                            <field name="doctor_fee"/>
                        </group>
                        <group>
                            <field name="doctor_registration"/>
                            <field name="doctor_pancard"/>
                            <field name="doctor_fiscalcode"/>
                            <field name="doctor_iban"/>
                            <field name="doctor_contractset"/>
                            <field name="doctor_contractrecieved"/> 
                        </group> 
                    </group>
                </page>
                <page string="Clients">
                   <field name="doctor_clients"/>
                </page>
            </notebook>
        </form>
    </field>
    </record>
<record id="view_doctor_tree_extended" model="ir.ui.view">
    <field name="name">crm.lead.doctor.tree</field>
    <field name="model">crm.lead.doctor</field>
    <field name="arch" type="xml">
        <tree string="Doctor Details" version="7.0">
                <field name="doctor_id"/>
                <field name="name"/>
                <field name="doctor_mobile"/>
                <field name="doctor_email"/>
                <field name="doctor_hospital"/>
        </tree>
    </field>
    </record>

    <record id="new_doctor" model="ir.actions.act_window">
        <field name="name">Doctors</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">crm.lead.doctor</field>
        <field name="view_type">form</field>
        <field name="view_mode">tree,form</field>
        <field name="view_id" ref="view_doctor_tree_extended"/>
    </record>

<!-- ===========================Menu Settings=========================== -->
<menuitem name ="Doctors - Hospitals" id = "menu_lead" />
  <menuitem name="Doctors" id="sub_menu_lead" parent="menu_lead" />
    <menuitem name="Doctors" id="create_lead" parent="sub_menu_lead" action="new_doctor"/>

</data>
</openerp>

2 个答案:

答案 0 :(得分:1)

这是医生 - 客户之间关系的完美范例。如果您在开幕式中意识到这一点,您将在医生视图中显示客户。

如果你的客户有一位以上的医生,那么很多人都会有意义。

小提示:'doctor_clients':fields.one2many('res.partner','doctor')

from qrcode import *
from osv import osv
from osv import fields

class res_partner(osv.osv):
 _inherit = "res.partner"
 _description = "adding fields to res.partner"
 _columns = { 

    'doctor': fields.many2one('crm.lead.doctor','Doctor'), #the so called relation_field for the one2many relation to crm.lead.doctor
 }
 class crm_lead_doctor(osv.osv):
    _name = "crm.lead.doctor"
    _order = "name"
    _columns ={
        'name':fields.char('Doctor Name',required=True,size=64,translate=True),
        'doctor_id':fields.char('Doctor Id',size=64,readonly=True),
        'doctor_mobile': fields.char('Mobile',required=True,size=64),
        'doctor_email': fields.char('Email',size=64),
        'doctor_hospital': fields.many2one('crm.lead.hospital','Hospital'),
        'doctor_street': fields.char('Street', size=128),
        'doctor_street2': fields.char('Street2', size=128),
        'doctor_zip': fields.char('Zip', change_default=True, size=24),
        'doctor_city': fields.char('City', size=128),
        'doctor_state_id': fields.many2one("res.country.state", 'State'),
        'doctor_country_id': fields.many2one('res.country', 'Country'),
        'doctor_brochure': fields.char('Brochuer',size=64),
        'doctor_flyer': fields.char('Flyer',size=64),
        'doctor_training': fields.char('Training',size=64),
        'doctor_starterpacksent': fields.char('Strater pack sent',size=64),
        'doctor_no_of_deliveries': fields.char('No of Deliveries/year',size=64),
        'doctor_fee': fields.char('Fee',size=64),
        'doctor_registration': fields.char('Registration No',size=64),
        'doctor_pancard': fields.char('Pan Card No',size=64),
        'doctor_fiscalcode': fields.char('Fiscal Code',size=64),
        'doctor_iban': fields.char('IBAN',size=64),
        'doctor_contractset': fields.char('Contract Set',size=64),
        'doctor_contractrecieved': fields.char('Contract Recieved',size=64),
        'doctor_clients':fields.one2many('res.partner','doctor','Clients') #here we use 'doctor' the new field of res.partner as relation_field to bind the relation
    }
    def create(self, cr, uid, vals, context={}):
        doc_seq = self.pool.get('ir.sequence').get(cr, uid, 'master.doctor')
        vals['doctor_id'] = doc_seq
        res = super(res_partner.crm_lead_doctor, self).create(cr, uid, vals, context)

观点很好             返回res

答案 1 :(得分:0)

如果我理解正确,您希望在医生的客户下看到客户?

只有在您保存客户后,客户才会在医生的客户下展示。在保存之前,不会创建链接,因此它不会显示在医生的表单视图中。

如果您保存客户,然后打开医生,它应该显示为客户。