I have a simple model Class
class Talk(models.Model):
url = models.URLField()
user = models.ForeignKey(User)
category = models.ForeignKey(Category)
description = models.TextField(blank=True, max_length=500)
title = models.TextField(blank=True)
and I connect to oembed api to get data from the url submitted
def get_oembed_info(self):
params = {'url': self.url, 'format': 'json'}
fetch_url = 'http://api.embed.ly/1/oembed?%s' % urllib.urlencode(params)
result = urllib.urlopen(fetch_url).read()
result = json.loads(result)
KEYS = ['title', 'type', 'url', 'description', 'provider_url', 'provider_name', 'width', 'height', 'html', 'thumbnail_url', 'author_url']
for key in KEYS:
if result.has_key(key):
setattr(self, key, result[key])
def save(self):
self.get_oembed_info()
super(Talk, self).save()
It’s ok to add a new Talk from Admin but when i try to update the same talk later nothing changes.
Also is this way a good to get the data and store it in DB.
Thanks
Each time you save, you fetch data from the server and overwrite your model fields. You could check whether it’s a new object before fetching: