Reputation: 162
Problem summary: I don't understand the syntax for objects.update_or_create
in Django.
CSV files have the field names in first row:
# data.csv
field1, field2,
12, 14,
35, 56,
I have a model for my postgresql database in models.py:
from django.db import models
# example model, real one has 100 fields of different types
class MyModel(models.Model):
field1 = models.IntegerField(blank=True, null=True)
field2 = models.IntegerField(blank=True, null=True)
Now I want to create/update the database when new data comes in in form of CSV files:
import csv
with open('./datafiles/data.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
update, created = MyModel.objects.update_or_create(
defaults={???},
???
)
update.save()
What goes into the question mark marked places?
The Docs say this.
Upvotes: 0
Views: 840
Reputation: 13731
As per the documentation.
obj, created = Person.objects.update_or_create(
first_name='John', last_name='Lennon',
defaults={'first_name': True},
)
If John Lennon already existed, then the first name would be changed to Bob. If it didn't exist, a person would be created with the name Bob Lennon. This handles the case of updating an existing record as well as the case of needing to create it in one neat line.
For your case, you need to determine what fields are you looking up records on and what are the fields you'll be updated based on that lookup. Typically the lookup fields are slugs or some combination of unique fields.
Please read the documentation to fully understand the gotcha's and the potential necessity of adding a unique (non-pk) field or unique together group of fields to your model.
Upvotes: 1