Forums

Automatically create child records

Since we can't use triggers on here with MySQL :( , I need to do it in Python

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 do this to Python, I modified the models.py as follows:

class Evaluation(models.Model):
...
def save_model(self, request, obj, form, change):
    for cat in Category.objects.all():
        evdet = EvaluationDetail.objects.create(evaluation=self.id, category=cat)
    obj.save()

But it doesn't work :( Any help would be greatly appreciated.

Here's a StackOverflow question that may help.