0

Every time there's an INSERT/UPDATE of this model I want to check that the 'personprofile' hasn't been committed to more a days work on that date ?

By 'committed' I mean I only want the update to continue if, after it's finished, the total of proportion_of_day_required on all instances of PersonRequirement with the same personprofile and the same date is 1.0 or less.

class PersonRequirement(models.Model):
    '''
    Joins a person to a Subproject on a given
    day and nominates how much of that day
    they are required for
    '''
    subproject = models.ForeignKey(SubProject, on_delete=models.CASCADE)
    personprofile = models.ForeignKey(PersonProfile, on_delete=models.CASCADE)
    date = models.DateField()
    proportion_of_day_required = models.DecimalField(max_digits=3, decimal_places=2)

I was looking at the save method on the model but I wasn't sure whether trying to read other instances of the same model would be OK while trying to insert/update an instance of the same model ?

Maybe there's a better way which I've overlooked ?

glaucon
  • 8,112
  • 9
  • 41
  • 63

2 Answers2

0

Use the save custom method for this one. The same custom method is used to create manual logic every time a save is done which also includes the update

Dean Christian Armada
  • 6,724
  • 9
  • 67
  • 116
0

You have to override the save method from your model, checking if saving meets your requirements, rejecting it if not.

About concurrent modifications, you will have to implement an optimistic lock (look at this thread), using versionning and rejecting update if another occured at the same time.

Hope I helped.

Community
  • 1
  • 1
Maxime B
  • 966
  • 1
  • 9
  • 30