How make this special many2many fields validation using Django ORM?

Posted by e-satis on Stack Overflow See other posts from Stack Overflow or by e-satis
Published on 2010-03-21T19:31:49Z Indexed on 2010/03/21 19:41 UTC
Read the original article Hit count: 369

Filed under:
|
|

I have the folowing model:

class Step(models.Model):

    order = models.IntegerField()
    latitude = models.FloatField()
    longitude = models.FloatField()
    date = DateField(blank=True, null=True)


class Journey(models.Model):

    boat = models.ForeignKey(Boat)
    route = models.ManyToManyField(Step)
    departure = models.ForeignKey(Step, related_name="departure_of", null=True)
    arrival = models.ForeignKey(Step, related_name="arrival_of", null=True)

I would like to implement the following check:

        # If a there is less than one step, raises ValidationError.

        routes = tuple(self.route.order_by("date"))

        if len(routes) <= 1:
            raise ValidationError("There must be at least two setps in the route")

        # save the first and the last step as departure and arrival
        self.departure = routes[0]
        self.arrival = routes[-1]

        # departure and arrival must at least have a date
        if not (self.departure.date or self.arrival.date):
            raise ValidationError("There must be an departure and an arrival date. "
                                  "Please set the date field for the first and last Step of the Journey")

        # departure must occurs before arrival    
        if not (self.departure.date > self.arrival.date):
            raise ValidationError("Departure must take place the same day or any date before arrival. "
                                  "Please set accordingly the date field for the first and last Step of the Journey")

I tried to do that by overloading save(). Unfortunately, Journey.route is empty in save(). What's more, Journey.id doesn't exists yet. I didn't try django.db.models.signals.post_save but suppose it will fail because Journey.route is empty as well (when does this get filled anyway?). I see a solution in django.db.models.signals.m2m_changed but there are a lot of steps (thousands), and I want to avoid to perform an operation for every single of them.

© Stack Overflow or respective owner

Related posts about django

Related posts about manytomany