例如,我有一个YouTube视频模型。
当管理员创建视频时,他将视频ID传递给管理界面,然后在第一次保存视频详细信息正在加载,管理员可以对此详细信息进行一些更改(例如,删除视频说明中的内容):
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
if self.id is None:
video_data = json.loads(urllib2.urlopen(
'https://www.googleapis.com/youtube/v3/videos?key=somekey&part=snippet&id=%s' % self.video_id).read())
video_info = video_data['items'][0]['snippet']
self.title = video_info['title']
self.description = video_info['description']
if 'thumbnails' in video_info:
if 'high' in video_info['thumbnails']:
self.thumb = video_info['thumbnails']['high']['url']
if 'maxres' in video_info['thumbnails']:
self.thumb_max_res = video_info['thumbnails']['maxres']['url']
else:
self.thumb_max_res = self.thumb
只有在完成此操作后(管理员编辑后)我才会向用户发送推送通知。
所以,问题是,我应该怎么做? (我的意思是我的代码应该在哪里做?)
答案 0 :(得分:1)
我认为你可以使用django signal GraphRequestAsyncTask request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
try {
fName2.setText(jsonObject.getString("first_name"));
fAvatar2.setProfileId(jsonObject.getString("profile_id"));<--I am not sure profile_id is the name there do this to see the JSON response
Log.e("FBGraphResponse", jsonObject.toString());
} catch(Exception e){e.printStackTrace();}
}
}).executeAsync();
方法。它旨在在保存对象后进行后期处理。我不建议覆盖post_save
方法,因为它很容易搞砸。 Django doc:https://docs.djangoproject.com/en/1.8/ref/signals/#post-save