Django fixtures - linking two models with OneToMany

174 Views Asked by At

I am pre-populating my Django app with two tables and using csvimport, which is great. The two models have a OneToMany relationship; each Testresult is related to a single School. There are multiple Testresults per School.

The two models have the class variable "psid" in common, which I want to use to link them.

Ex. models.py:

class School(models.Model):
    psid = models.CharField(max_length=5)  # it's in the form of AB001
    schoolname = models.CharField(max_length=200)

class Testresult(models.Model):
    psid = models.CharField(max_length=5)
    gradelevel = models.CharField(max_length=5)
    mathresult = models.DecimalField(max_digits=13, decimal_places=10)
    readingresult = models.DecimalField(max_digits=13, decimal_places=10)

My question is, given that I'm importing the data, how do I make sure these two tables are linked by the "psid" column? If I were not pre-populating I would use ForeignKey, however I don't believe that will work here. Likewise, NaturalKeys don't seem like they would necessarily link the two models together with "psid" as desired.

0

There are 0 best solutions below