0

I parse data from a restful API call using django-rest-framework ModelSerializer. Here is the code:

 url = "API URL HERE" r = requests.get(url) json = r.json() serializer = myModelSerializer(data=json, many=True) if serializer.is_valid(): serializer.save() 

Here is the modelSerializer:

class myModelSerializer(serializers.ModelSerializer): class Meta: model = MyModel 

MYModel:

class MyModel(models.Model): City = models.NullBooleanField(default=False, null=True) Status = models.CharField(max_length=100, null=True) stateName = models.CharField(max_length=50) marketingName = models.TextField(null=True) county = models.CharField(max_length=200, null=True) 

My problem is I need to find out what field value changed from the last time I called the restful api and updated data. OR If there is any new records. How do I achieve this?

1
  • This has more to do with django and little to do with django-rest-framework. Commented Aug 5, 2016 at 0:47

1 Answer 1

2

First, you could add a column to your MyModel:

updated = models.DateTimeField(auto_now=True) 

This will update whenever an instance is changed. If you filter on this field in your queryset, you can determine what rows changed and if there are new rows.

Finding out what field changed is harder, but here is an idea--add a string field to the model and write a custom save method for your model, like this:

class MyModel(models.Model): City = models.NullBooleanField(default=False, null=True) Status = models.CharField(max_length=100, null=True) stateName = models.CharField(max_length=50) marketingName = models.TextField(null=True) county = models.CharField(max_length=200, null=True) updated = models.DateTimeField(auto_now=True) updated_fields = models.CharField(max_length=200, null=True) def save(self, *args, **kwargs): if not self.pk: # if this is new, just save super(MyModel, self).save(*args, **kwargs) else: # get the original old = MyModel.objects.get(id=self.pk) # make a list of changed fields changed_fields = [] for field in self._meta.get_all_field_names(): if getattr(self, field, None) != getattr(old, field, None): if field not in ['updated', 'updated_fields']: changed_fields.append(old) # make a comma separated string self.updated_fields = ','.join(changed_fields) super(MyModel, self).save(*args, **kwargs) 

Now the updated_fields column will contain the set of fields that were last updated.

Sign up to request clarification or add additional context in comments.

6 Comments

When i do serializer.save() I get the error that myModel with the id already exists. looks like the serializer won't let me update the same row.
What does the json you are serializing look like?
Was it giving you that error before you put in the custom save method?
The error is when I call this: if serializer.is_valid(): serializer.save() The serializer is not valid because the record with the pk already exists.
Here is the data from api looks like : <ArrayOfCommunity xmlns:i="w3.org/2001/XMLSchema-instance" xmlns=""> <Community> <Id xmlns="">10015620120207</Id> <City>South Park</City> <County>Allegheny</County> <state>MD</state> </Community> </ArrayOFCommunity>
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.