PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, August 30, 2022

[FIXED] How to use update_or_create when updating values in Django database from CSV files

 August 30, 2022     csv, django, django-models, python     No comments   

Issue

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.


Solution

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.



Answered By - schillingt
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing