Search code examples
pythonodoo-8odoo

Using domain with datetime and date


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.


Solution

  • 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.