Display many2one field in OpenERP view - view

There's a field of line_ids in the class of 'purchase.requisition' :
'line_ids' : fields.one2many('purchase.requisition.line','requisition_id',
'Products to Purchase',states={'done': [('readonly', True)]}),
And I add a many2one field to 'purchase.order.line' :
'requisition_line_id': fields.many2one('purchase.requisition.line' ,
u'Requisition Line',readonly = True , ondelete = 'restrict' ),
Now , how can I display then name field of 'purchase.requisition' in the view of 'purchase.order.line' ?

You can you fields.related.
Example :
class hr_employee(osv.osv):
_name = "hr.employee"
_columns = {
'address_id': fields.many2one('res.partner.address', 'Working Address'),
'city': fields.related('address_id', 'city', type='char', string='City'),
}
Then after add moduel_view.xml at appropriate place.
<field name="city" />
Here you see an example of fields.related.
Now similar try to fix your problem.
I hope this will be helpful for you.

Goto View Inheritance It will help you a lot.

Related

Odoo duplicate many2many fields

A model [m] with a many2many field [m2m]
Field [m2m] have attribute [copy=False]
A form view [v] display the [m] and the field [m2m] with widget [many2many_tags]
I have tested that the python object did return a with False on field [m2m]
but the javascript, keep render the tags on web.
model.py
class model(models.Model):
_name = "m"
m2m = fields.Many2many('sale.order', string="SO", copy=False)
XML File
<record ...>
<form>
<group>
<field name="name"/>
<field name="ref"/>
<field name="so_m2m" widget="many2many_tags" options="{'no_create_edit': True}"/>
</group>
</form>
</record>
When I duplicate the current record, the field [m2m] is keeps the old tag from old record which is not expected.
After clicking the save button, the old tag will disappear.
Try one thing here :
Whatever your third table is there, both fields used in many2many relation, from third table keep them as a copy false.
i.e.
m2m = fields.Many2many('sale.order','sale_order_another_table_rel', 'sale_order_field','antother_table_field', string="SO", copy=False)
Into new table :
_name = 'sale.order.another.table.rel'
sale_order_field_id = // copy=false
another_table_field_id = //copy = false
Hopefully work.

How to invisible a field based on complex condition in odoo10?

I add a field in account.payment.
bank_id = fields.Many2one('res.bank', string="Bank")
when i clicked the register payment button in account.invoice then it is opened a register payment wizard.
I want to invisible the bank_id based on the condition
journal_id.type not in bank
How it is possible in odoo??
in xml I add a condition like this.
attrs="{'invisible': [('journal_id.type', 'not in', ['bank'])]}"
How to correct the condition??
in this situation you can not apply direct attrs so you have to take one compute boolean field to check the journal type and based on that field make bank_id visible or invisible
in account.payment
from odoo import models, fields, api, _
class account_payment(models.Model):
_inherit = "account.payment"
bank_id = fields.Many2one('res.bank', string="Bank")
check_journal = fields.Boolean(string="Journal Type", compute='_check_journal_type')
#api.depends('journal_id')
def _check_journal_type(self):
if self.journal_id.type == 'bank':
self.check_journal = False
else:
self.check_journal = True
in account.payment view
<field name="check_journal" invisible="1"/>
<field name="bank_id" attrs="{'invisible': [('check_journal', '=', True)]}"/>

Required validation on hidden field

I am using client side validation on hidden field in asp.net MVC. I am using required validation using data annotations. I am trying to validate hidden field but it’s not working.
My Model
[Required(ErrorMessage = "From date is required")]
public DateTime? FromDate { get; set; }
My View
#Html.HiddenFor(m => m.FromDate, new { ID = "hfdFromDate" }
#Html.ValidationMessageFor(m => m.FromDate)
I would like to know how I can achieve the same, any small inputs on the same is also greatly appreciated.
Thanks in advance.
As some have pointed the validation isn't performed because the "hidden" property is set.
I had the same issue and ended up using the style tag:
#Html.TextBoxFor(m => m.Foo, new { style = "visibility:hidden"})
The validation is then performed.
Added this line before form.valid().
form.data("validator").settings.ignore = "";
where form is your form element.
In my case :
var form = $("#myForm");

Displaying yes or no instead of 1 or 0 in MVC view

Is there a way I can annotate a field of my view so that it displays yes instead of one and no instead of 0 in my view? I know there is a display attribute that takes the name and displays something different, but that is not what I am looking for.
Create Display Template with name "YesNo"
#model int
#(Model == 0 ? "No" : "Yes")
And add attribute to field of your model
[UIHint("YesNo")]
public int Value { get; set; }
When you will display your model then "YesNo" display template will be used.
#Html.DisplayFor(model => model.Value) // output "Yes" or "No"
You have a few options. The simplest is to add some "display logic" in your View.
#(Model.YesNo == 1 ? "Yes" : "No")
I would create an Enum type and use it instead of an int on your model.
public enum YesNo
{
No = 0,
Yes, 1
}
Then your view would simply have
#Html.DisplayFor(model => model.YesNo)

Specify size and maxlength for Html.TextBoxFor

I need to change the size of textbox :
#Html.SimpleTextBoxFor(m => ((ModifiableProperty<string>)m).Value.TheCurrentValue, new { id = fieldId})
I tried this
#Html.SimpleTextBoxFor(m => ((ModifiableProperty<string>)m).Value.TheCurrentValue, new { id = fieldId, #maxlength = "100" })
but doesn't work.
You can try this too :
#Html.SimpleTextBoxFor(m => ((ModifiableProperty<string>)m).Value.TheCurrentValue, new { id = fieldId, style ="width:200px"})
Just change the 200px value for the size you want.
For maxlength I use the same syntax as you and it is working for me.
#Html.TextBoxFor(model => model.EL_Taille_Initiale, new { style = "width:50px", #maxlength = "5" })
Take out the "#" character for your maxlength attribute. You only need that for reserved keywords (i.e. class). Also, you don't need the quotes around the number for maxlength.
#Html.SimpleTextBoxFor(m => ((ModifiableProperty<string>)m).Value.TheCurrentValue, new { id = fieldId, maxlength = 100 })
If that doesn't solve the problem, then please post what the HTML markup is being generated on the response page.
I am using a constructor for my TextBox that does not allow passing HTML attributes, so I had to add this to my $(document).ready function: $('#textBoxID').attr('maxlength', 30);
Doesn't directly answer the OP question, but offers an alternate starting point.

Resources