1

I need to convert a Trigger on a MySQL table to Python/Django.

The Trigger loads a select into a cursor, loops through and creates the child records. Here it is:

DECLARE cur CURSOR FOR select id from eval_category;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur;
  det_loop: LOOP
    FETCH cur INTO ids;
    IF done THEN
      LEAVE det_loop;
    END IF;
    INSERT INTO eval_evaluationdetail 
    (evaluation_id, category_id) VALUES (NEW.id,ids);
  END LOOP;
CLOSE cur;

To convert this to Python, I modified the models.py as follows:

def save(self):
    super(Evaluation, self).save()
    for cat in Category.objects.all():
        self.EvaluationDetail.evaluation=self
        self.EvaluationDetail.category=cat
        self.EvaluationDetail.save()

This is the latest iteration, but it still doesn't work :( Any help would be greatly appreciated.

mykl
  • 41
  • 5
  • but why do you want to? – e4c5 Aug 15 '15 at 13:17
  • This is a scheduling program so when the evaluation is scheduled, I want to automatically create the child records, (100 records) in the Detail file (the categories (1-100). The Trigger worked fine on my local install, but the hosting site doesn't allow Triggers (grrrr!) So I need to replicate the process the Trigger did in Django. – mykl Aug 15 '15 at 18:58
  • Ah that makes it complicated! but perhaps you might want to consider switching the hosting provider instead of changing code. They might be having lots more pleasant surprises like this hidden away. – e4c5 Aug 16 '15 at 01:27
  • Thanks, my thoughts exactly. – mykl Aug 16 '15 at 19:51

1 Answers1

0

Firstly, save_model is for ModelAdmin, not Model. It won't do anything special.

You have two options: you can override save on the model, or make use of the post_save signal. Here are some links to other answers taking both approaches:

This answer has a good example using signals,

This question has a good example of overriding save, including a discussion of how you might choose between the two options.

Community
  • 1
  • 1
maackle
  • 2,074
  • 2
  • 15
  • 27
  • Hey, sorry if I seem obtuse, but I'm not well versed in Python and my Trigger was so simple to do :) I've changed it to: `def save(self): self.save() for cat in Category.objects.all(): evdet = EvaluationDetail.objects.create(evaluation=self.id, category=cat)` now I get an error maximum recursion depth exceeded. Should I use super instead of self.save()? – mykl Aug 14 '15 at 21:28
  • Ok, I read through those and tried both with varied errors. this is the last iteration: `def save(self, **kwargs): super(Evaluation, self).save() if self.pk is None: for cat in Category.objects.all(): self.EvaluationDetail.evaluation=self self.EvaluationDetail.category=cat self.EvaluationDetail.save()` any idea? I'm not sure about the self.pk, but I only want it to run on an insert. – mykl Aug 15 '15 at 19:02
  • You were right about using `super`. As for rest of the function, in your comment above you said you're trying to create a bunch of child records, but here you're just reassigning different values to the same record over and over and saving. (I'm not sure if EvaluationDetail is a model, or an instance). You probably need something more like `EvaluationDetail.objects.create(...)` in your inner loop. – maackle Aug 17 '15 at 18:55
  • Hi thanks, EvaluationDetail is a model `class EvaluationDetail(models.Model): evaluation = models.ForeignKey(Evaluation) category = models.ForeignKey(Category) score = models.ForeignKey(Scoring)` So instead of the self. I should use `EvaluationDetail.objects.create(evaluation_id=self, category_id=cat)` – mykl Aug 17 '15 at 19:32