Odoo Help


How do I check for overlapping dates?

Martin King
on 2/12/15, 6:00 PM 1,335 views

I've created my first module (a campsite reservation module) and have everything functioning as I'd like except I haven't been able to figure out how to make a constraint to prevent date overlap on a campsite booking.

I have two models, Campsite and Booking.  I've linked to Sales for customer information.  Booking contains start_date, end_date, site_id, customer_id and duration fields.  When making a new booking, I'd like a constraint that checks to see if the requested campsite is already booked for the requested date range.  I have some basic Python and SQL knowledge but I can't figure out a constraint that checks "incoming" dates against "existing" dates in the database.  I know we're supposed to be concise, but thanks for any help!

Baiju's answer below solved my problem! Thanks very much, Baiju! I don't have enough karma yet to reply to his post. I've been using the Odoo 8 module building tutorial and the constraint syntax is different to what Baiju offered. I'm guessing this is from the Openerp 7 API?

Martin King
on 2/13/15, 12:23 PM

Using your original answer, I managed to get this working using the new API: @api.one @api.constrains('start_date', 'end_date') def _check_date_overlap(self): for date in self: date_ids = self.search([('start_date', '=', date.start_date), ('site_id', '=', date.site_id.id), ('id', '', date.id)]) if date_ids: raise exceptions.ValidationError("Dates overlap, please choose different dates.") Thanks again for your help!

Martin King
on 2/13/15, 2:00 PM

Baiju KS

--Baiju KS--
Baiju KS
| 5 5 8
Kochi, India
--Baiju KS--
ODOO technical

        I like learning new thing, especially technical thing. I am a quick learner, I can learn technical things very fast. I have good logical skill,  I have immense interest in programming.

e-mail id : baijuks@hotmail.com

Baiju KS
On 2/13/15, 2:15 AM

Hi Martin,

You can use this script:-

    _constraints = [
        (_check_date, 'You can not book  same day!', ['start_date','end_date']),

    def _check_date(self, cr, uid, ids):
        for item in self.browse(cr, uid, ids):
            item_ids = self.search(cr, uid, [('start_date', '<=', item.end_date), ('end_date', '>=', item.start_date),       ('site_id', '=', item.site_id.id), ('id', '<>', item.id)])
            if item_ids:
                return False
        return True

Hope this helps. If any querys please let me know.

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 2/12/15, 6:00 PM
Seen: 1335 times
Last updated: 3/16/15, 8:10 AM