-1

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.

e4c5
  • 52,766
  • 11
  • 101
  • 134
LuckyStarr
  • 1,465
  • 2
  • 12
  • 14

2 Answers2

0

As for me, it seems better to write short function (in separate file) that updates database:

import json
from app.models import Student, Classes

data = json.loads('fixtures.json')
for student in data['students']:
    # parse student; save classes and student objects

print('done')

(2) I think you should use "many to many" relation, not "one to many"

Satevg
  • 1,601
  • 2
  • 16
  • 23
  • You are suggesting creating a separate function as response to my question (3) ? That could probably work. Now I don't understand why many to many relationship ? May be I need a third model called "studentClasses" which has primary key of student and the class it takes and grade for that class ? Updated my question by adding question (4). – LuckyStarr Feb 18 '17 at 03:48
0

You need to add a field student_classes to Student, you can serialize it using jsonpickle. By having that field, I don't think you would need any foreign keys...You are talking about "loading tables" - that seems is where you are also confused. The model in django consists of classes just as your modeling code shows, therefore, to work with the models (and data in them), you would be importing the classes, such as "from models import Student, Classes".

postoronnim
  • 486
  • 2
  • 10
  • 20
  • Wondering how student_classes would help ? This fields needs to have class ID and grade, may be I need a third model to take care of this ? I've added question (4), does it makes sense ? About loading table, I already have bunch of data in JSON file, want to know what's the best way to get them into the database. – LuckyStarr Feb 18 '17 at 03:53
  • I think the best way for you to approach this is to envision the problem: you will need to be able to look up student data based on classes and vice versa. You can accomplish that by adding student_classes field, which connects both. To insert data, write a script that opens a file with data, imports the classes, and saves data into each field with class.save() method? Does this make it clearer? – postoronnim Feb 19 '17 at 17:40
  • Thanks, visualizing helped, I will create a third model for student_classes and write a script that loads data into these models. – LuckyStarr Feb 20 '17 at 20:48