Right, or even in a standalone script if a migration is more than you need: 
https://docs.djangoproject.com/en/stable/topics/settings/#calling-django-setup-is-required-for-standalone-django-usage

On Thursday, August 18, 2016 at 2:14:39 AM UTC-4, Todor Velichkov wrote:
>
> Maybe a data migration 
> <https://docs.djangoproject.com/en/1.9/topics/migrations/#data-migrations> 
> is what you are looking for.
>
> On Thursday, August 18, 2016 at 8:38:33 AM UTC+3, Aaron Weisberg wrote:
>>
>> 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
>>
>> On Wednesday, August 17, 2016 at 3:48:18 PM UTC-5, Tim Graham wrote:
>>>
>>> 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
>>>
>>> On Wednesday, August 17, 2016 at 1:42:04 PM UTC-4, Aaron Weisberg wrote:
>>>>
>>>> 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
>>>>
>>>>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/7600ae91-ae54-4d33-9dab-83a0322194fc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to