How to limit the characters (to show in tree view) of text field in odoo - treeview

I have a text field.
description = fields.Text('Description')
I want to show this field in tree view, but not the whole value of the field.
I mean if my field value is "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", I just want to show
"AAA..." in tree view.
How to achieve this? Thanks.

Please use the below xml code for the field
xml : <tree string="Tree String" class="my_class"> <field name="description" />
in css write below:
css : .my_class [data-id="description"]{width: 200px;}

You can define a computed field to render just a few characters from the "description" field, and expose it in the tree view (as read-only)

Related

xpath remove an attribute from an dynamic attribute list

I want remove an xml attribute via xpath, but
the xml element could have more atrributes in the future.
html code:
<p class="red, blue, green">test/<p>
xpath:
<xpath expr="//p[contains(#class, 'green')]" position="attributes">
<attribute name="class">red, blue</attribute>
</xpath>
Is where a better way for fixtext "red, blue"?
In order to suppport possible new version of the html file like
"<p class="red, blue, green, brown">test</p>" in the future without need to change the xpath code again.
for instance actual attribute list as var + an xpath function
What about setting the #class to
concat(substring-before(#class, "green"), substring-after(#class, "green"))
You'll need to solve the abandoned commas, too, but as Björn Tantau commented, in real HTML the classes would be separated by spaces, so you can just wrap the result into normalize-space.

Odoo 8: Nested form view of Many2one field

I am in the process of refactoring a model (let's call it Parent), which stores a number of Many2one fields that reference records of the same model (Child). The Parent form view contains a notebook and each page displays details for a specific Child, currently described within the page in a repetitive fashion and with lots of related fields. I'm trying to avoid the repetition in the view, and get rid of the need for the related fields.
class Child(models.Model):
_name = "child"
# ...
class Parent(models.Model):
_name = "parent"
child_1 = fields.Many2one('child', 'Child 1')
child_1_age = fields.Float(related='child_1.age', string='Child Age')
# ...
child_2 = fields.Many2one('child', 'Child 2')
child_2_age = fields.Float(related='child_2.age', string='Child Age')
# ...
<!-- ... -->
<field name="model">parent</field>
<field name="type">form</field>
<field name="arch" type="xml">
<notebook colspan="8" col="8">
<page>
<!-- buttons, a bunch of related Child fields, etc -->
</page>
<page>
<!-- buttons, a bunch of related Child fields, etc -->
</page>
</notebook>
</field>
I have a special form view defined for the Child, but I don't know how to insert it in the notebook pages of the Parent view. Since it is possible to insert tree views in forms (like for One2many fields for example), I guess there has to be a way to do it with forms as well. An example how to achieve this effect would be greatly appreciated.
Please ignore any syntax errors, the above is just a simple visual representation to help better describe my case.
You can use the widget attribute in order to assign a widget to your relational field, for example
<field name="my_field" widget="my_widget"/>
To see an example as to how a widget works and is created take a look at the following example:
Go to addons/account/project/wizard/account_analytic_journal_report_view.xml and see the line that defines a many2many_tags widget.
This form widget is assigned on addons/web/static/src/js/view_form.js at around line 6396 that comments Registry of form fields ...
The many2many_tags string that we used is assigned a actual widget, that widget has a template, and that template is rendered in place of your field.
TL;DR Give a widget element to your field, define that widget, assign that widget to work on a template and create the template that contains your view.

XPath - Nested path scraping

I'm trying to perform html scrapping of a webpage. I like to fetch the three alternate text (alt - highlighted) from the three "img" elements.
I'm using the following code extract the whole "img" element of slide-1.
from lxml import html
import requests
page = requests.get('sample.html')
tree = html.fromstring(page.content)
text_val = tree.xpath('//a[class="cover-wrapper"][id = "slide-1"]/text()')
print text_val
I'm not getting the alternate text values displayed. But it is an empty list.
HTML Script used:
This is one possible XPath :
//div[#id='slide-1']/a[#class='cover-wrapper']/img/#alt
Explanation :
//div[#id='slide-1'] : This part find the target <div> element by comparing the id attribute value. Notice the use #attribute_name syntax to reference attribute in XPath. Missing the # symbol would change the XPath selector meaning to be referencing a -child- element with the same name, instead of an attribute.
/a[#class='cover-wrapper'] : from each <div> element found by the previous bit of the XPath, find child element <a> that has class attribute value equals 'cover-wrapper'
/img/#alt : then from each of such <a> elements, find child element <img> and return its alt attribute
You might want to change the id filter to be starts-with(#id,'slide-') if you meant to return the all 3 alt attributes in the screenshot.
Try this:
//a[#class="cover-wrapper"]/img/#alt
So, I am first selecting the node having a tag and class as cover-wrapper and then I select the node img and then the attribute alt of img.
To find the whole image element :
//a[#class="cover-wrapper"]
I think you want:
//div[#class="showcase-wrapper"][#id="slide-1"]/a/img/#alt

XPath Wildcard -- Any Node Name, Must have Specific Attribute Value

I am having difficulty figuring out an XPath query that would allow me to return nodes based on the value of the Program attribute in the example below. For example, I would like to be able to search all nodes for a value of the Program attribute = "011.pas". I tried /Items/*[Program="012.pas"] and also /Items/Item*[Program="01.pas"] but neither works. What is the correct expression?
<Items>
<Item0 Program="01.pas"></Item0>
<Item1 Program="011.pas"></Item1>
</Items>
The attribute is selected with #Program, the child elements of the Items element with /Items/*, so you want /Items/*[#Program = '011.pas'].
Try this :
/items/*[#Program='011.pas']

Validating input type number in html5

I want to restrict entry of input onto a field of type number such that input cannot be outside range of min-max specified in the html.
input type = "number" min = "1" max = "5"
Is there a way of outputting the number field without the text box and i would rather not use
"input type = range"
as slider does not show value currently selected
Please help.
Thanks.
Based on what you said, I suggest using a simple input text field and check it's value validity on submission via JavaScript (as #Kush mentions above). You could also check it as the user types, or moves focus away from that field.
<form>
Only 1 to 100 <input type="text" name="number" pattern="\d{1,2}(?!\d)|100" title="one to hundreed only">
<input type="submit">
</form>

Resources