Python dictionaries saved to models

54 views
Skip to first unread message

Aaron Weisberg

unread,
Aug 17, 2016, 5:42:04 PM8/17/16
to Django users
Hi everyone,

I have several dictionaries that I would like to import into my django project database, but I can't figure out exactly how.

I have tried to implement through fixtures, through views and through loaddata, but I must be doing something wrong at every turn- so if someone could suggest a workflow, it would really help me a lot!

My data is currently in a list of dictionaries called data:

Enter code here...data = [{'pos': 'position', 'player': 'name', 'ht': 'height', 'wt': 175, 'birth': 'September 9, 1967', 'college': 'university', 'number': 10, 'exp': 1},


each dictionary within that list is a "player" who I'd like to input his data into my model "Players"

Enter code here...
#models.py
class Player(models.Model):
number = models.CharField(max_length=2)
player = models.CharField(max_length=50)
position = models.CharField(max_length=2)
height = models.CharField(max_length=50)
weight = models.CharField(max_length=50)
birth = models.DateField()
exp = models.CharField(max_length=50)
college = models.CharField(max_length=50)

def __str__(self):
    return self.player

There are about 500 players in that list of dictionaries above that I would like to insert into this model.

1) Can i run a function in the model above to insert the data into its appropriate field?  If so, how do I execute that function?
2) If it's a view that I need to execute-  do I just put the function in views.py?  How then is the dictionary imported into the database?

or... How would you go about getting that data into your database?  Please note that the data is coming from a url that might change from time to time.  But I think once i get the first iteration of data into these models I can figure out how to update.  But the only way I can figure out is to create them one by one in the shell or create a form and input them one by one.

If you have a solution, please share and be as detailed as possible since I am a complete beginner with django.    

Peace and love,

Aaron

Tim Graham

unread,
Aug 17, 2016, 8:48:18 PM8/17/16
to Django users
You need to transform your data dictionary so that the keys match the models field names and then you can use:

players_data = [{'number': 1, ...}, {...}]

for data in players_data:
    Player.objects.bulk_create(**data)

(or something similar with bulk_create())

https://docs.djangoproject.com/en/stable/ref/models/querysets/#bulk-create

Aaron Weisberg

unread,
Aug 18, 2016, 5:38:33 AM8/18/16
to Django users
Great- thanks Tim.

I understand the Python, but what I guess where I'm fuzzy is where to exactly put that code.  

Do I put it in the models.py? class.py?  and how do I execute?

That is the final piece of the puzzle for me.  

Thanks so much.

Aaron

Todor Velichkov

unread,
Aug 18, 2016, 6:14:39 AM8/18/16
to Django users
Maybe a data migration is what you are looking for.

Tim Graham

unread,
Aug 18, 2016, 12:48:58 PM8/18/16
to Django users

Aaron Weisberg

unread,
Aug 18, 2016, 1:40:26 PM8/18/16
to Django users
Thanks.  

However, when I write the function and try to run the data migration I run into this error:

Enter code here...NameError: name apps is not defined

That is coming from this line in the migration:

Players = apps.get_model("team","Players")

This is a copy/paste from the documentation.  Is there a typo?

Thanks.

Aaron Weisberg

unread,
Aug 18, 2016, 2:10:43 PM8/18/16
to Django users
Nevermind- the good folks at Django didn't add:

from django.apps import apps in order to pull off this data migration

Tim Graham

unread,
Aug 18, 2016, 6:37:01 PM8/18/16
to Django users
`apps` should be the first argument to the RunPython function. Using the import you mentioned is incorrect and may cause bugs if your models change in the future.
Reply all
Reply to author
Forward
0 new messages