Model fields not updated
I wanted to add facebook feed to my website (only the first latest wall message). So I have created the model :
class FacebookFeed(models.Model):
checked = models.DateTimeField(name="Time Checked", auto_now_add=True)
message = models.TextField(name="Message", null=True)
author = models.CharField(name="Author", max_length=50)
date = models.DateTimeField(name="Post time", null=True)
def save(self, *args, **kwargs):
queryset = FacebookFeed.objects.all()
if(queryset):
return
else:
#super(FacebookFeed, self).save()
super(FacebookFeed, self).save(*args, **kwargs)
def check_time(self):
if self.checked:
now = datetime.utcnow()
diff = now - self.checked
if diff.seconds >= 3600:
return True
return False
I want to perform feed update only each full hour so that's what check_time method is for. Now the function being used in my view :
def getSocialFeed():
#facebook
fb = FacebookFeed.objects.all()
created = False
if not fb:
logging.debug('not fb')
f = FacebookFeed();
开发者_如何学JAVA f.save()
created = True
fb = FacebookFeed.objects.all()[0]
if fb.check_time() or created:
logging.debug('check time true')
conn = httplib.HTTPSConnection("graph.facebook.com")
conn.request("GET", '/UserName/feed')
r = conn.getresponse()
jsonResult = r.read()
json = simplejson.loads(jsonResult)
json = json['data']
notAuthor = False
i = 0
if json:
logging.debug('json')
while notAuthor is not True:
logging.debug('while')
if json[i]['from']['name'] == 'UserName':
logging.debug('result %s' % (json[i]['from']['name'] == 'UserName'))
logging.debug('message: %s' % json[i]['message'])
date = json[i]['created_time'].replace('T', ' ')
logging.debug('date: %s' % date)
date = date[0:-5]
logging.debug('date2: %s' % date)
logging.debug('fb: %s' % fb)
fb.message = json[i]['message']
fb.date = datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
fb.author = 'UserName'
try:
x = fb.save()
logging.debug('x: %s' % x)
notAuthor = True
logging.debug(fb.message)
logging.debug(notAuthor)
except Exception, e:
logging.debug('e: %s' % e)
else:
i+=1
f = FacebookFeed.objects.all()[0]
logging.debug('msg: %s' % f.message)
I've got the json response parsed, extracted all the data I want but somehow my object is not saved corretcly. Any ideas why ?
This is the debug log :
2011-05-27 14:15:24,671 DEBUG not fb
2011-05-27 14:15:24,681 DEBUG check time true
2011-05-27 14:15:26,607 DEBUG json
2011-05-27 14:15:26,607 DEBUG while
2011-05-27 14:15:26,607 DEBUG result True
2011-05-27 14:15:26,607 DEBUG message: sometext
2011-05-27 14:15:26,607 DEBUG date: 2011-05-26 14:42:36+0000
2011-05-27 14:15:26,607 DEBUG date2: 2011-05-26 14:42:36
2011-05-27 14:15:26,607 DEBUG fb: FacebookFeed object
2011-05-27 14:15:26,608 DEBUG x: None
2011-05-27 14:15:26,608 DEBUG sometext
2011-05-27 14:15:26,608 DEBUG True
2011-05-27 14:15:26,608 DEBUG msg: None
def save(self, *args, **kwargs):
queryset = FacebookFeed.objects.all()
if(queryset):
return
else:
#super(FacebookFeed, self).save()
super(FacebookFeed, self).save(*args, **kwargs)
i think if(queryset)
is always True
if it contains records or not, to check for empty queryset, use queryset.count() == 0
instead
精彩评论