Skip to Content
Menu
This question has been flagged
3 Replies
5560 Views

Hi Guys, In my Medical Lab project I wanted to use SQL CONSTRAINTS to prevent the same patient of having two appointments in the same day however the appointment_date field is a datetime field which means the validation of SQL constraints works only if the patient would have more than one appointment in the same exact date and the same exact time along with seconds.

but I want the validation to be based on the day month year only.


_sql_constraints = [
('name_code_unique', 'unique (patient_id, appointment_date)', 'An Appointment For the Same Patient Has Already Been Booked On The Same Date'),
]


any one would help please




Avatar
Discard
Best Answer

Approach 1:

change your field from date = fields.Datetime to fields.Date

you can override the field by inheriting the model.

Approach 2:

override create and write functions to validate date field.

The "create" function example comes below. You have to override the "write" method as well.

from odoo import models, api

import dateutil.parser
from dateutil.relativedelta import relativedelta
from odoo.exceptions import ValidationError


class YourClass(models.Model):
_name = 'module.name'

@api.model
def create
(self, vals_list):
date = vals_list.get('date', None)
if date:
date = dateutil.parser.parse(date).date()
record = self.env['sale.order'].search([('date', '>=', date), ('date', ', date + relativedelta(days=1))])
if record:
raise ValidationError('An appointment is already set for this user on corresponding day.')
return super(YourClass, self).create(vals_list)


Avatar
Discard

EDIT:
record = self.env['sale.order'].search([('date', '>=', date), ('date', '<', date + relativedelta(days=1))])

Best Answer

Hello..

I think you could the attr date.date:

Suppose your variable its called date = fields.Datetime(), using date.date and you will get date only from this variable.

Avatar
Discard
Best Answer

Hi,

In Odoo we have fields DateTime and Date . You can use

field_without_time = fields.Date(string=" ")

for saving date only. In this method the database will only store date.So, you can easily get the date without time.
When we use sql constraints , it will directly check the values from the database. So, if you are using

field_name_with_datetime = fields.DateTime(string=" ")

you have to store the field with only date in the database. You can use the .date() function to get a date from datetime. It can be done just by using an onchange function. So , whenever you choose the first field, the second field will save with only a date. And you can add this field into sql constraints.

Example

@api.onchange('field_name_with_datetime')
def _onchange_field_name_with_datetime(self):
    if self.field_name_with_datetime:
    self.field_without_time = self.field_name_with_datetime.date()

And change sql constraints like this:

_sql_constraints = [('name_code_unique', 'unique (patient_id, field_without_time)', 'An Appointment For the Same Patient Has Already Been Booked On The Same Date'),]

Regards

Avatar
Discard
Related Posts Replies Views Activity
2
Mar 15
10400
0
Aug 20
4220
1
Jun 17
1282
2
Nov 15
6579
2
Sep 21
3500