Apply filter to t-foreach - odoo-11

Is there any t-filter tag capability in odoo 11 qweb template?
sample :
<tr t-foreach="o.line_ids" t-as="line_ids" t-filter="line_ids.name == 'car'">

You can use 'filtered' for filtering the records. So, you can do it like this:
<tr t-foreach="o.line_ids.filtered(lambda x: x.name == 'car')" t-as="line_ids">
According to the example from Odoo ORM API documentation:
records.filtered(lambda r: r.company_id == user.company_id)
filtered()
returns a recordset containing only records satisfying the provided predicate function. The predicate can also be a string to filter by a field being true or false:
This can also be applied in Qweb reports.
You can read more about this here - https://www.odoo.com/documentation/online/reference/orm.html

Related

Ruby on Rails - Criteria - Mongoid - where condition with 2 by 2 columns

I'm working with RoR + MongoDB (Mongoid) and I need to apply a filter using or condition by 2 columns.
I saw some recommendations like this one but didn't work.
query = query.where(name: /#{attributes[:name]}/i).or(query.where(email: /#{attributes[:email]}/i))
If I use this one, looks like the or will be applied for all conditions:
query = query.where(name: /#{attributes[:name]}/i).or(email: /#{attributes[:email]}/i)
Anyone has any suggestion?
From what I understand the correct query should be:
query = query.or([{name: /#{attributes[:name]}/}, {email: /#{attributes[:email]}/i}])
This will return documents that has a name property with the value from attributes[:name] or documents that has email property with the value from attributes[:email]
Does this answer your question?

How to search records with a string which contains some characters of the target field string in Odoo v10?

I am using Odoo v10. While scanning a barcode, a string contains some characters of a char field value. For example,
A field value ('tracknum') = "20171103"
Search the field by entering a string "xxxxxx20171103" or "xxxx20171103yyy"
is there any way to do it?
I have modified the search view :
<field name="tracknum" string="Tracknum" filter_domain="..."/>
How to dig out related records?
You can create an auxiliar computed field like this
custom_name = fields.Char(
string='Custom',
compute='_compute_custom_name',
search='_search_custom_name'
)
#api.multi
#api.depends()
def _compute_custom_name(self):
''' The field has to be a computed field
You do not need to do anything here
'''
pass
def _search_custom_name(self, operator, value):
''' Actually this converts a domain into another one.
With this new domain Odoo can search well
Arguments:
* operator: if you are searchig words it is going to be ilike
* value: the string ro search
The method could return something like this
* [('id', 'in', id_list)]
'''
all_records = self.search([]) # recordset with all the values of the current model
ids = []
if operator == 'ilike':
ids = all_records.filtered(lambda r: r.tracknum in value).mapped('id')
return [('id', 'in', ids)]
Then you can add this field to the search view like this:
<field name="custom_name" string="Tracking Number" />
Keep in mind that it is not a stored field, so it is going to be very inefficient. And you should iterate over all the values each time you want to make a search.
Once you have added the field to the search view it shoul look like this, Tracking Number should appear in the field name

sapui5 filter based on isNull or isNotNull

I need to show all data where column data is Null or NOT Null, that is similar to SQL operator ISNULL. I have tried with sap.ui.model.filteroperator but not getting anything compare for NULL. If you know the solutions please help.
As #Prashob said, you can use the filter construct. You can use it with the "null" keyword as well. Example:
oTable.getBinding("items").filter([new Filter("property", "EQ", null)])
Or in an XML view:
<Table items="{
path: '/collection',
filters: [{path: 'property', operator: 'EQ', value1: null}]
}">
If you are using a JSONModel, then this will work without any issues. If you are using an OData service, then it depends a little on the backend. Normally, it should work (e.g. see the Northwind service; it supports null comparison: http://services.odata.org/Northwind/Northwind.svc/Customers?$filter=Region%20eq%20null); the underlying implementation should know to translate the OData filter into an appropriate WHERE clause (i.e. IS NULL instead of = NULL)
I would suggest you project on the column of your VDM view to a new calculated attribute. Converting the value based on isnull function to a Empty string.
if(isnull("Column"),'',"Column")
You can use Filter
for example,
EQ : new Filter(your_path, FilterOperator.EQ, ""); // Show only those fields whose values is equal to empty
NE: new Filter(your_path, FilterOperator.NE, ""); // Show only those fields whose values is not equal to empty
String Data Type can hold null and "" value
and hence depends on if you want to filter both
you would need to query for both i.e.
oTable.getBinding("items").filter([
new Filter("property", "EQ", null),
new Filter("property", "EQ", "")
])
non-String data types:
can hold only null and hence you can query
oTable.getBinding("items").filter([new Filter("property", "EQ", null)])

Print report if condition in Qweb Odoo 8

I'm working with Qweb and Odoo 8, I've created my report in RH module, the problem is how can i print this report if condition
in hr_contract I've added a one2many fields
_columns = {
'contract_job_ids': fields.one2many(
'hr.contract.job',
'contract_id',
'Jobs',
),
I want print this report just if the len(object.contract_job_ids) >= 2
If you want to run specific code before your report prints (or not) you can create an Abstract model which defines a render_html function so that your function will run when printing the report rather than the generic odoo function. This is referenced in the documentation
HERE
Take a look at this example.
from openerp import models, fields, api, exceptions
class YourReport(models.AbstractModel):
_name = 'report.your_addon.report_template_id'
#api.multi
def render_html(self, data=None):
report_obj = self.env['report']
report = report_obj._get_report_from_name('your_addon.report_template_id')
docs = self.env['your_addon.your_model'].browse(self._ids)
for doc in docs:
if not len(doc.object.contract_job_ids) >= 2:
raise exceptions.ValidationError("You cant run this report\nYou need more contracts!")
docargs = {
'doc_model': report.model,
'docs': docs,
}
return report_obj.render('your_addon.report_template_id', docargs)

Rails 4 and Mongoid: programmatically build query to search for different conditions on the same field

I'm building a advanced search functionality and, thanks to the help of some ruby fellows on SO, I've been already able to combine AND and OR conditions programmatically on different fields of the same class.
I ended up writing something similar to the accepted answer mentioned above, which I report here:
query = criteria.each_with_object({}) do |(field, values), query|
field = field.in if(values.is_a?(Array))
query[field] = values
end
MyClass.where(query)
Now, what might happen is that someone wants to search on a certain field with multiple criteria, something like:
"all the users where names contains 'abc' but not contains 'def'"
How would you write the query above?
Please note that I already have the regexes to do what I want to (see below), my question is mainly on how to combine them together.
#contains
Regex.new('.*' + val + '.*')
#not contains
Regex.new('^((?!'+ val +').)*$')
Thanks for your time!
* UPDATE *
I was playing with the console and this is working:
MyClass.where(name: /.*abc.*/).and(name: /^((?!def).)*$/)
My question remains: how do I do that programmatically? I shouldn't end up with more than two conditions on the same field but it's something I can't be sure of.
You could use an :$and operator to combine the individual queries:
MyClass.where(:$and => [
{ name: /.*abc.*/ },
{ name: /^((?!def).)*$/ }
])
That would change the overall query builder to something like this:
components = criteria.map do |field, value|
field = field.in if(value.is_a?(Array))
{ field => value }
end
query = components.length > 1 ? { :$and => components } : components.first
You build a list of the individual components and then, at the end, either combine them with :$and or, if there aren't enough components for :$and, just unwrap the single component and call that your query.

Resources