0

I'm new to Django and I have a question (Yes, yes, I was looking for and did't find ...). There are two models:

class CounterDetail(models.Model):
counter = models.ForeignKey(Counter, on_delete=models.CASCADE)
date_placing = models.DateField(auto_now=False, auto_now_add=False, blank=False, null=False)
verifying_period_mounts = models.IntegerField(blank=False, null=False)

and

class Detail(models.Model):
counterdetail = models.ForeignKey(CounterDetail, on_delete=models.CASCADE)
date_last_verification = models.DateField(auto_now=False, auto_now_add=False, blank=False, null=False)
date_obxoda = models.DateField(auto_now=False, auto_now_add=False, blank=False, null=False)

How to implement the check when adding an entry to the second model through the admin panel: field date_obxoda = models.DateField(auto_now=False, auto_now_add=False, blank=False, null=False)more then date_placing = models.DateField(auto_now=False, auto_now_add=False, blank=False, null=False) from another model.

I'm looking for a method (preferably as an overridden save () method in the model) when adding records via the admin panel, to verify that the date from the second model (with a foreign key) is greater than the first.

Please give an example, thank you.

  • Django provide a [ManyToManyField](https://docs.djangoproject.com/en/2.0/topics/db/examples/many_to_many/), so you don't need Foreign key referencing to each other in both model. Also could you please explain a bit more what you want to do ? – Sumeet Kumar Jan 24 '18 at 17:59
  • Hah.) They do not refer to each other. The first model refers to a completely different table, but that's another story ...I added a paragraph with a more detailed description. – Aleks Porberevskiy Jan 24 '18 at 18:15
  • ohh.. my bad, I didn't notice. well posted an answer, check it. – Sumeet Kumar Jan 24 '18 at 18:23
  • related: https://stackoverflow.com/q/23361057, https://stackoverflow.com/q/25647498 – djvg Sep 14 '22 at 17:02

1 Answers1

0

you can override Detail model save method like

def save(self, *args, **kwargs):
    super(Detail, self).save(*args, **kwargs)
    if self.date_obxoda > self.counterdetail.date_placing :
        #do_something

let me know if this help

Sumeet Kumar
  • 969
  • 1
  • 11
  • 22