I have tried to use the update_or_create() method assuming that it would 
either, create a new entry in the db if it found none or update an existing 
one if it found one and had differences to the defaults passed in  - or 
wouldn't update if there was no difference.  However it just seemed to 
recreate entries each time even if there were no changes.

I think the issue was that I wanted to:
1)  get an entry if all fields were the same,
2) or create a new entry if it didn't find an existing entry with the 
unique_id
3) or if there was an entry with the same unique_id, update that entry with 
remaining fields. 

The update_or_create() method doesn't seem to work as I had hoped using how 
I have called it below - it just always seems to do an update if it finds a 
match on the given kwargs. 

Or if I tried passing in all That would 
would have to be passing in all the fields as keyword args to check that 
nothing had changed but then that would miss option 3) finding an existing 
entry that 






supplier, created = 
Supplier.objects.update_or_create(unique_id=product_detail['supplierId'],
                                                       defaults={
                                                           'name': 
product_detail['supplierName'],
                                                           'entity_name_1': 
entity_name_1,
                                                           'entity_name_2': 
entity_name_1,
                                                           'rating': 
product_detail['supplierRating']})





class Supplier(models.Model):
    unique_id = models.IntegerField(unique=True)
    name = models.CharField(max_length=255, unique=True)
    entity_name_1 = models.CharField(max_length=255, blank=True)
    entity_name_2 = models.CharField(max_length=255, blank=True)
    rating = models.CharField(max_length=255)

    last_updated = models.DateTimeField(auto_now=True)


def __str__(self):
return self.name


Not being convinced that update_or_create() would give me what I needed I made 
the below function:


def create_or_update_if_diff(defaults, model):
    try:
        instance = model.objects.get(**defaults)
        # if no exception, the product doesn't need to be updated
    except model.DoesNotExist:
        # the product needs to be created or updated
        try:
            model.objects.get(unique_id=defaults['unique_id'])
        except model.DoesNotExist:
            # needs to be created
            instance = model.objects.create(**defaults)
            # model(**defaults).save()
            sys.stdout.write('New {} created: {}\n'.format(model, 
instance.name)) 
            return instance, True
        else:
            # needs to be updated
            instance = model.objects.update(**defaults)
            sys.stdout.write('{}:'
                             ' {} updated \n'.format(model, 
instance.unique_id)) 
            return instance, True
    return instance, False


However I can't get it to be quite right.  I key a key error on update possibly 
because the defaults passed in now include unique_id. Should the unique_id be 
separated and both passed into the function to fix this?  (And should I have 
created a function to achieve this - or would have update_or_create() have been 
able to do this.?)



supplier_defaults={
                                                           'unique_id': 
product_detail['supplierId'],
                                                           'name': 
product_detail['supplierName'],
                                                           'entity_name_1': 
entity_name_1,
                                                           'entity_name_2': 
entity_name_2,
                                                           'rating': 
product_detail['supplierRating']}



-- 
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/a0b6e1dd-d583-480e-9c6e-540c1ad4511a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to