0

Hi I'm trying to filter a model using domain like this

class TableOne(models.Model):
    _name = "table.one"

    date = fields.Datetime(string="Date", default=fields.Datetime.now)

class ReportOne(models.Model):
    _name = "report.one"

    date_from = fields.Date(string="From")

    date_to = fields.Date(string="To")

    def do_some_filtering(self, date_from, date_to):
        table_one = self.env["table.one"]

        domain = []
        domain.append(("date", ">=", date_from ))
        domain.append(("date", "<=", date_to ))

        recs = table_one.search(domain) // zero results, even though the date_from / date_to range is inside the date spread

<record id="some_view_id" model="ir.ui.view">
    <field name="name">some.view.name</field>
    <field name="model">report.one</field>
    <field name="arch" type="xml">
        <form create="false">
            <sheet>
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-md-6">
                            <group string="Date Range">
                                <field name="date_from" />
                                <field name="date_to" />
                            </group>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <button string="Create" type="object" name="do_some_filtering" />
                        </div>
                    </div>
                </div>
            </sheet>
        </form>
    </field>
</record>

I have tried using strptime , strftime no luck. How is datetime/date filtering done in Odoo?

I have included the view code. I am very sorry this is still not the complete code. I have to remove unnecessary codes that are not related to the issue. Because if I include all the code, it is going to be very long code sample. I feel it is enough to understand the issue. Please let me know if you still can not understand it.

strike_noir
  • 4,080
  • 11
  • 57
  • 100
  • What's your input? What is `models.Model`? What's your expected output? – FHTMitchell Jun 06 '18 at 09:28
  • `models.Models` is the base class model of Odoo framework. `ReportOne` recs field is expected to be filled with `TableOne` records filtered with domain. – strike_noir Jun 06 '18 at 10:11

2 Answers2

2

what you are doing here don't make any sense in that point of execution all fields are empty False or an object.

This search is done while you are executing the class statement so if you acces an atribute directly like that you get a False or an object of datetime class witch is not like standar datetime labrary.

You can confirm what i'm saying by printing both field value before search. What you want exactly in that point we coulf help to rewrite your code.

Edits*

Dates in Odoo are a text. You are using Date field to look for datetimes I think Odoo will concatenate ' 00:00:00' to your value. If you choose the same date will always have record that are dated exactly at hout 00:00:00.

You can change your value

      [....     '=>',  self.from_date + ' 00:00:00'),
             ...., self.to_date + ' 23:59:59')] 

And keep in mind that odoo deal with timezone let say that your region is GMT + 1, IF you choose 2018-01-01 00:00:00 in database the value will be converted to 2017-12-31 23:00:00 be carful with this facts Odoo is an international systems and datetimes are managed by Odoo on this base.

Charif DZ
  • 14,415
  • 3
  • 21
  • 40
  • I'm sorry, my code must have been confusing. I have added more lines to the code sample. Please have a look. – strike_noir Jun 12 '18 at 08:08
  • You are not calling the method any where. You can explain what you want in your question. – Charif DZ Jun 12 '18 at 08:27
  • I call it in a view. The view has the fields `date_from` and `date_to`. The value sent to the function correctly. I know this because I have checked the value using `ipdb`. The issue is when apply it to domain. – strike_noir Jun 12 '18 at 08:30
  • Converte your dates to date time by just adding the hour part. And be careful with timezone good luck. Check my edits – Charif DZ Jun 12 '18 at 10:16
0

Here there is an incompatible search with date object and datetime object. so you should make date_from and date_to to datetime field and input in same format. else date field + 00.0000.00 as per format will be added with search and you get []

Hilar AK
  • 1,655
  • 13
  • 25