postgresql - Django bulk_create update on duplicate fields -


i have model mirrors foreign data source, , foreign data changes old records. bulk_create objects foreign data speed, cannot find documentation updating on duplicate fields.

example models:

class author(models.model):     first_name = models.charfield(max_length=255)     last_name = models.charfield(max_length=255)  class book(models.model):      author = models.foreignkey('author')     title = models.charfield(max_length=255)     price = models.floatfield()  # changes      class meta:         unique_together = ('author', 'title') 

running in creation loop slow (several minutes few thousand records) because every book needs 2 transactions: 1 get_or_create author, get_or_create book. possible use bulk_create , update price when book matches on author , title?

in following suppose foreign data source 'morphological equivalent' following:

books = [{'title': 'title1', 'author': 'author name 1', 'price': 23.50},          {'title': 'title2', 'author': 'author name 2', 'price': 24.50}, ...] 

then can update model in way

books_queryset = book.objects.select_related()  book in books:    try:      item = books_queryset.get(title=book['title'], author__last_name=book['author'])      item.price = book['price']      item.save()    except objectdoesnotexist:      # add new item 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -