This question has been flagged
2 Replies
4809 Views

I wanted to know if I want to create a new field then how this can be done. How to set the xml and other details .I think I will have to set them and return the field. But is there any specific function to create new field ? . I have no idea .

Please help Thanks

Avatar
Discard

You want to add dynamic field ?

Author

first I want to add a new field then I will think a way to add dynamic ones . I f you know any of these plz help

Author

my field is not being displayed

Best Answer

Hi

This will help you https://accounts.openerp.com/forum/Help-1/question/47546

Refer below code

def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
    if context is None:
        context = {}
    wiz_id = self.pool.get('ir.actions.act_window').search(cr, uid, [("name","=","analytic.plan.create.model.action")], context=context)
    res = super(account_analytic_plan_instance,self).fields_view_get(cr, uid, view_id, view_type, context, toolbar=toolbar, submenu=submenu)
    journal_obj = self.pool.get('account.journal')
    analytic_plan_obj = self.pool.get('account.analytic.plan')
    if (res['type']=='form'):
        plan_id = False
        if context.get('journal_id', False):
            plan_id = journal_obj.browse(cr, uid, int(context['journal_id']), context=context).plan_id
        elif context.get('plan_id', False):
            plan_id = analytic_plan_obj.browse(cr, uid, int(context['plan_id']), context=context)

        if plan_id:
            i=1
            res['arch'] = """<form string="%s">
<field name="name"/>
<field name="code"/>
<field name="journal_id"/>
<button name="%d" string="Save This Distribution as a Model" type="action" colspan="2"/>
"""% (tools.to_xml(plan_id.name), wiz_id[0])
            for line in plan_id.plan_ids:
                res['arch']+="""
                <field name="account%d_ids" string="%s" nolabel="1" colspan="4">
                <tree string="%s" editable="bottom">
                    <field name="rate"/>
                    <field name="analytic_account_id" domain="[('parent_id','child_of',[%d])]" groups="analytic.group_analytic_accounting"/>
                </tree>
            </field>
            <newline/>"""%(i,tools.to_xml(line.name),tools.to_xml(line.name),line.root_analytic_id and line.root_analytic_id.id or 0)
                i+=1
            res['arch'] += "</form>"
            doc = etree.fromstring(res['arch'].encode('utf8'))
            xarch, xfields = self._view_look_dom_arch(cr, uid, doc, view_id, context=context)
            res['arch'] = xarch
            res['fields'] = xfields
        return res
    else:
        return res
Avatar
Discard
Author

I tried that .But I was unable to get that solution . I think fields_view_get will up grade the already created fields . I want to create fields not upgrade.

using fields_view_get get you can add dynamic filed. can u paste your code here ?

Author

have u created dynamic fields before?

yes ? i have

Author

def fields_view_get(self, cr, uid, view_id, view_type='form', context=None,toolbar=False,submenu=False): result = super(deg_form, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar,submenu) moves_fields=result['fields'] moves_fields.update({ "name" : {'string': 'MyNewName','type': 'char'}}) moves_fields.update({ "data_type" : {'string': 'MyTYPE','type': 'char'}})
moves_fields.update({ "father_name" : {'string': 'MyNewName','type': 'char'}}) # school_obj = self.pool.get('deg.form') result['fields'] = moves_fields # if vi

Author

above here is my function

Author

<button name="fields_view_get" string="CREATE FIELD" type="object" /> And this is button which will call fields_view_get to create fields

Author

can u make me realize whats wrong here

@arsalan: fields view get call when view load, you want create in button click. So in your case i can't help you.

Author

is there some way I could do this , by any function . If I can return field

Author

hello jack?

Sorry i have no idea..

Author

ok tell me how can I create fields with fields_view_get function

I have update ans check try it.

Author

can we give name, data_type etc of our choice or not?

yes you can type:'Any tyep' , string:'name of field'

Author

but here u have not used my class n objects , I am having problems understanding it. How could I import it in my module

Author

u have used account.journal , journal_id , account_analytic_plan etc . can u explain a bit

Author

because I will have to make changes according to my class.

There is lost of example in openerp addons module.

Author

ur code is complaining "xml view error". May be u have missed something. Please check

This code is given in openerp addons>>account_analytic_plans>>account_analytic_plans.py line no : 170 to 210, pls refer this

Author

just tell me how it works if u know. because I really need to understand this to code it

Author

can u code a simple function n edit the one that I sent u . It will be much easier for me to understand the flow of fields_view_get because ur function has an xml issue

show your code will give solution.

Author

I sent u my code before .Pls do make changes in it

Author

xml issue, because u have not given the xml issue

Best Answer

def fields_view_get(self, cr, uid, view_id, view_type='form', context=None,toolbar=False,submenu=False): result = super(deg_form, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar,submenu) moves_fields=result['fields'] moves_fields.update({ "name" : {'string': 'MyNewName','type': 'char', 'size': 10}) moves_fields.update({ "data_type" : {'string': 'MyTYPE','type': 'char', 'size': 10}) moves_fields.update({ "father_name" : {'string': 'MyNewName','type': 'char', 'size': 10})

Avatar
Discard