Django:如何获取查询集的相关对象?(Django: How to get related objects of a queryset?)
假设我有两个模型:
A: pass B: a = foreign_key(A)
现在,我有一个查询集
bs = B.objects.filter(...)
我想要得到所有的bs,这意味着每个a都由b引用,其中b在bs中。
有没有办法做到这一点? 我认为在SQL中,一个简单的连接就可以了,我不知道django是否支持这个。
Assume I have two models:
A: pass B: a = foreign_key(A)
Now, I have a query set
bs = B.objects.filter(...)
I want to get all the a of bs, which means every a which is referenced by b for which b is in bs.
Is there a way to do so? I think in sql, a simple join will do, I don't know if django support this.
原文:https://stackoverflow.com/questions/36197425
最满意答案
不要使用
callbacks
,这是过去。$http
服务返回承诺对象,这是更方便的选项(错误处理,承诺链接):app.factory('socialMedia', ['$http', function($http){ return { fetchInstagram: function() { var url = "https://api.instagram.com/v1/users/*******/media/recent?client_id=*****&callback=JSON_CALLBACK"; return $http.jsonp(url).then(function(response) { return response.data; }); } } }]);
确保你从
fetchInstagram
方法返回Promise。然后在控制器中,你会像这样使用它:
socialMedia.fetchInstagram().then(function(data) { $scope.data = data; });
Don't use
callbacks
, it's the past.$http
service returns promise object, which is much more convenient option (error handling, promise chaining):app.factory('socialMedia', ['$http', function($http){ return { fetchInstagram: function() { var url = "https://api.instagram.com/v1/users/*******/media/recent?client_id=*****&callback=JSON_CALLBACK"; return $http.jsonp(url).then(function(response) { return response.data; }); } } }]);
Make sure you return Promise from
fetchInstagram
method.Then in controller you would use it like this:
socialMedia.fetchInstagram().then(function(data) { $scope.data = data; });
相关问答
更多-
调试和调查后,如果回调文件执行速度不够快,我发现该调用是从instagram发送两次。 基于文档 : 此外,您应该在2秒内超时确认POST - 如果您需要对收到的信息进行更多处理,则可以在异步任务中进行。 他们会发送第二次请求,以防他们在2秒内没有收到第一次请求的回复。 最后,我有一个空白的callback.php文件,里面只有“sleep”,每次调用两次。 After debugging and investigation i found that call is sent from instagram ...
-
不要使用callbacks ,这是过去。 $http服务返回承诺对象,这是更方便的选项(错误处理,承诺链接): app.factory('socialMedia', ['$http', function($http){ return { fetchInstagram: function() { var url = "https://api.instagram.com/v1/users/*******/media/recent?client_id= ...
-
Instagram Real-Time API回调网址转义[重复](Instagram Real-Time API Callback URL Escaping [duplicate])[2021-04-04]
回调网址很好。 您看到的额外信息是由于JSON编码/字符的方式。 查看这里的逃避规则: http : //www.json.org/ 真正的问题是Instagram的实时订阅API似乎在过去几天都有问题。 请参阅@ DanShev对链接的评论。 与此同时,我有一个系统,可以定期查看用户的照片,查看我感兴趣的照片。 我的代码(在Python中)看起来像这样: from instagram import client users = InstagramUser.objects.all() for user in ... -
确保Curl选项 CURLOPT_SSL_VERIFYPEER设置为false。 在代码中找到Curl请求,并添加或编辑一个如下所示的选项: $ch = curl_init(); ... curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); <<-- Add this line Ultimately I resolved the problem. now I am able to connect to Instagram. I use the CodeIgni ...
-
哦,这样可以解决问题。 在循环结果后需要循环遍历数组。 //instagram api example, this code makes you fetch images after searching specific hashtag $(document).ready(function() { var myId = "------"; //sets so form doesnt override jquery $('#submit').click(funct ...
-
什么是instagram api中的回调url以及如何实现它(What is callback url in instagram api and how to can I implement it)[2022-10-30]
“回叫网址”(也称为redirect_url )是Instagram会在用户登录Instagram 后发送给用户的URL,并授权您的应用程序读取其数据。 这是OAuth 2规范的一部分,Instagram(和许多其他人)使用它来控制对其API的访问。 OAuth 2“流程”的简短版本如下: 您可以通过您的应用程序ID和重定向网址将用户发送到Instagram的页面。 用户在其页面上登录Instagram并授权您的应用程序。 然后,Instagram会将用户发送回您的应用程序(使用重定向URL值)以及可用于访 ... -
获取XMLHttpRequest无法在第二次api调用Instagram上加载(Getting XMLHttpRequest cannot load on second api call Instagram)[2022-05-01]
你必须添加另一个&callback=? for next_url 像这样修改并尝试它的工作原理: if(next_url!=undefined) { url=next_url + "&callback=?"; pagination(url); console.log(data); } You have to add another &callback=? for next_url Modify like this and try it works: if(ne ... -
没关系,所以显然你只是回应了hub.challenge就是这样 getInstagramSubscription(req,res){ console.log(req.query) //Just send back the hub.challenge return res.send(req.query['hub.challenge']); } 我还有一些正确的回调网址错误 Nevermind, so apparently you just respond with w ...
-
调用Instagram用户API时发生错误。(Error occurs while calling Instagram user API. Just died in GET Method)[2022-03-23]
+ user +应该是一个id不是instagram username https://api.instagram.com/v1/users/jygood/?callback= 看起来你使用的是jygood用户名而不是user_id ,它应该是一个整数值 the + user + should be an id not instagram username https://api.instagram.com/v1/users/jygood/?callback= looks like you are us ... -
要注销用户,您应该只删除令牌。 如果用户不希望您的应用访问其数据,则会取消您的应用访问权限。 如果您想为用户提供从应用程序注销的方法,您可以在登录实现上执行此操作(当然,如果您有后端),否则只需将用户发送回登录屏幕并删除令牌即可。 To logout the user you should only delete the token. If the user doesn't want your app to access their data they will cancel your app access ...