views:

15

answers:

1

Hi everyone, I've been playing with Django for a couple of days and stumbled upon the following issue. I have the following models:

Employee = (id, employeeName)
Project = (id, projectName)
Assignment = (fk_employee, fk_project, from_date, to_date)

What I want to do is to create a validator that won't allow assignment periods to overlap. The algorithm is very simple, but I don't know how to properly set it using validators.

What I thought is to have something like:

class Assignment(models.Model):
    ...
    from_date = models.DateField(
        validators=[DateInRangeValidator(%theFromDateValue%, %aReferenceToTheEmployee%)])
    to_date = models.DateField(
        validators=[DateInRangeValidator(%theToDateValue%, %aReferenceToTheEmployee%)])

def DateInRangeValidator(dateValue, employee):
    ...

BTW, I wrote these %theDateValue% and %aReferenceToTheEmployee% because I don't know how to send those parameters. Would it be 'self', 'from_date' or something else?

So, am I close to the solution? Thanks in advance! :)

A: 

You need access to more than one field of the model, and that's a model validation rather than a field validation.

So you may consider overriding the clean method of the Assignment class.

This part of the Django document may be helpful for you.

Satoru.Logic
Thanks for answering :) As per the documentation this seems to be the solution. I'll play with it for a while and I'll post back later.