Can I process Django fixture data before it is written to the database?

60 Views Asked by At

Let us say you have a simple model with a mandatory foreign key:

class Pupil(models.Model):
    name = models.CharField(max_length=40)
    teacher = models.ForeignKey(Teacher)

I want to load Pupil instances from a fixture file but add a default teacher in my code such that the teacher must not be entered in the fixture file:

- model: models.Pupil
  pk: 1
  fields: { name: "Max" }

I tried to assign the teacher in init like this:

def __init__(self):
    super().__init__()
    self.teacher = Teacher()

and similar things also in save but without success.

0

There are 0 best solutions below