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