2

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.

wsvincent
  • 207
  • 4
  • 13
  • 1
    I have no idea how django-csvimport works, but if it doesn't support ForeignKey relationships throw it away. You don't model your application around a csv library. Also, on the project's README mentions something about Foreign Keys, did you try what it says? – rantanplan Dec 03 '13 at 23:56
  • @rantanplan I appreciate your response. I think I worded my question poorly. What I was searching for--and just found--was "db_column" which allows me to set the specific column that is used for the Foreign Key relationship. – wsvincent Dec 04 '13 at 02:29
  • Glad it worked out in the end :) – rantanplan Dec 04 '13 at 14:54

0 Answers0