Have to make django models and take a JSON file to feed all the data for a student and classes display webapp. JSON file is what going to drive my modeling, it looks like this (truncated to couple of data points)...
{
"students": [
{
"first": "John",
"last": "Smith",
"email": "johnsmith@mailinator.com",
"studentClasses": [
{
"id": 1,
"grade": 4
},
{
"id": 2,
"grade": 3
},
]},
{...#truncated data, this follows with more students
"classes": {
"1": "Math 101",
"2": "English 101",
"3": "Science 101",
#total 8 classes declared, truncated
}
I have my data models as.....
class Student(models.Model):
first = models.CharField(max_length=200)
last = models.CharField(max_length=200)
email = models.EmailField()
class Classes(models.Model):
student = models.ForeignKey(Student)
class_name = models.CharField(max_length=50)
Here are my questions...
(1) How can I model in a way that takes in studentClasses:[{id:1, grade:4}] type relational input from JSON file and populates my database tables ? It seems I might have to declare serializer, why and how?
(2) Getting confused by ID in classes table and not ID in students table, do I explicitly have to declare primary key in modeling with ID in classes but not students models ?
(3) It seems I can load tables with "python manage.py load data myjsonfile.json" (would that be sufficient)?
(4) Do I need a third model called 'studentClasses' that keeps track of which student has taken what class and grade for that class ? Thanks in advance for your help.