Calling a studio field ? Odoo V14 - odoo

I would like to know if it's possible or not to call an Odoo studio field in a model like this ?
class sale_order(models.Model):
_inherit = "sale.order"
#api.onchange('x_studio_first_field')
def _onchange_firstfield(self):
if self.x_studio_first_field:
self.x_studio_second_field = self.x_studio_first_field
Thanks by advance

Apparently, the answer is what i've write in exemple. I tried it and it works !
class sale_order(models.Model):
_inherit = "sale.order"
#api.onchange('x_studio_first_field')
def _onchange_firstfield(self):
if self.x_studio_first_field:
self.x_studio_second_field = self.x_studio_first_field

Related

Extend States for Model in Odoo 11

I want to extend the "state" column of the mrp.production model. There was an example I found in https://www.odoo.com/de_DE/forum/how-to/developers-13/how-to-extend-fields-selection-options-without-overwriting-them-21529 but it seems to not work in odoo 11.
i.e. the __init__ signature changed to __init__(self, pool, cr) (guessing this from the error trace that I saw referencing model.__init__(pool, cr))
from odoo import models, fields
import logging
_logger = logging.getLogger(__name__)
class mrp_production(models.Model):
_inherit = 'mrp.production'
def __init__(self, pool, cr):
super(mrp_production, self).__init__(pool, cr)
states = self._columns['state'].Selection
_logger.debug('extend state of mrp.production')
state_other = ('other_state', 'My State')
if state_other not in states:
states.append(state_other)
The Error I'm receiving is:
AttributeError: 'mrp.production' object has no attribute '_columns'
You don't need to extend the __init__. Please look into the documentation to get a first look how to extend odoo business models.
For your example the correct code:
from odoo import models, fields
class MrpProduction(models.Model):
_inherit = 'mrp.production'
state = fields.Selection(
selection_add=[('other_state', 'My State')])
The tutorial you follow is for old API.
You can try
https://www.odoo.yenthevg.com/extend-selection-odoo-10/
from odoo import models, fields, api, _
class HrEmployee(models.Model):
_inherit = 'hr.employee'
gender = fields.Selection(selection_add=[('transgender', 'Transgender')])

How can I add a field in a model in odoo 9 new API?

I am trying to add a new field in the model of SaleOrderLine (Official Sale module).
It works perfect with the old API:
from openerp import _
from openerp.osv import osv, fields
class SaleOrderLineExt(osv.osv):
_inherit = ['sale.order.line']
_columns = {
'my_field_code': fields.float(string='My field Code'),
}
But, If I try to use the new API, the field is not created in the database.
from openerp import api, fields, models, _
class SaleOrderLineExt(models.Model):
_inherit = ['sale.order.line']
my_field_code = fields.Float(string='My field Code'),
I have read the Odoo new API guideline and it appears that my code is right, but it is not working.
What am I doing wrong?
Try with following code.
from openerp import api, fields, models, _
class SaleOrderLineExt(models.Model):
_inherit = 'sale.order.line'
my_field_code = fields.Float(string='My field Code')
Remove , at the end of field declaration.
Just Remove the semicolon which is at the end of field. You code will definitely work.

odoo- add custom fields in advaced search?

i'm trying to add product description to purchase advanced search menu
class purchase_order_line(osv.osv):
_inherit = 'purchase.order.line'
_columns = {
'name':fields.text(string='Description')
}
Your question is not clear for me. But i noticed an error in your code. You missed an equal sign after _columns. Try below code.
class purchase_order_line(osv.osv):
_inherit = 'purchase.order.line'
_columns = {
'name':fields.text(string='Description')
}

django Autocomplete-light how to choose a specific method from a mode

I am new at django and autocomplete-light. I try to get a different fields of the model from autocomplete-light, but it always return the same field. And the reason is because def in the Model defined one field. So I created another def, but can not make autocomplete-light to call that specific def. Here is my code.
models.py:
class Item(models.Model):
...
serial_number=models.CharField(max_length=100, unique=True)
barcode=models.CharField(max_length=25, unique=True)
def __unicode__(self):
return self.serial_number
def bar(self):
return self.barcode
.......
autocomplete_light_registry.py
autocomplete_light.register(Item,
name='AutocompleteItemserial',
search_fields=['serial_number'],
)
autocomplete_light.register(Item,
name='AutocompleteItembarcode',
search_fields=['barcode'],
)
Here is the issue: when I try to get the barcodes from the autocomplete-light, it returns serial_numbers. No matter what I try to get from the Item model, it always returns the serial number. I really appreciate for the answers. Thank you.
Just in case, here is the form.py
forms.py
class ItemForm(forms.ModelForm):
widgets = {
'serial_number': autocomplete_light.TextWidget('AutocompleteItemserial'),
'barcode': autocomplete_light.TextWidget('AutocompleteItembarcode'),
}
Although this is an old post but as I just faced the same issue therefore I am sharing my solution.
The reason autocomplete is returning serial_number is because django-autocomplete-light uses the __unicode__ method of the model to show the results. In your AutocompleteItembarcode all that is being done is autocomplete-light is searching by barcode field of Item.
Try the following.
In app/autocomplete_light_registry.py
from django.utils.encoding import force_text
class ItemAutocomplete(autocomplete_light.AutocompleteModelBase):
search_fields = ['serial_number']
model = Item
choices = Item.objects.all()
def choice_label(self, choice):
"""
Return the human-readable representation of a choice.
"""
barcode = Item.objects.get(pk=self.choice_value(choice)).barcode
return force_text(barcode)
autocomplete_light.register(ItemAutocomplete)
For more help you can have a look at the source code.

Many2one value display in OpenERP

I tried to create a simple Many2one field but the values are showing in this format: new.base,1 and new.base,2 and so on. Please let me know the fix so as to display value for the same.
class latest_base(osv.osv):
_inherit = ['mail.thread']
_name='latest.base'
_columns={
'name':fields.char('Name',required=True),
'image': fields.binary("Image", help="Select image here"),
'email':fields.char('Email'),
'code':fields.many2one('new.base','code'),
}
latest_base()
class new_base(osv.osv):
_name='new.base'
_columns={
'code':fields.char('Department'),
'hod':fields.char("Head of the Department"),
}
new_base()
try this, name is a Special fields in OpenERP and unique name used by default for labels in forms, lists, etc. If we don't use a name in table than we use _rec_name to specify another field to use.
class latest_base(osv.osv):
_inherit = ['mail.thread']
_name='latest.base'
_columns={
'name':fields.char('Name',required=True),
'image': fields.binary("Image", help="Select image here"),
'email':fields.char('Email'),
'code':fields.many2one('new.base','code'),
}
latest_base()
class new_base(osv.osv):
_name='new.base'
_rec_name = 'code'
_columns={
'code':fields.char('Department'),
'hod':fields.char("Head of the Department"),
}
new_base()
Hope this will solve your problem.
because you have not declared name field in your model, openerp returns name field value by default, if you want set other field as just define _rec_name='field_name' you will get value of that field.