首页 \ 问答 \ Django:如何获取查询集的相关对象?(Django: How to get related objects of a queryset?)

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
更新时间:2022-01-20 15:01

最满意答案

不要使用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= ...
  • 回调网址很好。 您看到的额外信息是由于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 ...
  • “回叫网址”(也称为redirect_url )是Instagram会在用户登录Instagram 后发送给用户的URL,并授权您的应用程序读取其数据。 这是OAuth 2规范的一部分,Instagram(和许多其他人)使用它来控制对其API的访问。 OAuth 2“流程”的简短版本如下: 您可以通过您的应用程序ID和重定向网址将用户发送到Instagram的页面。 用户在其页面上登录Instagram并授权您的应用程序。 然后,Instagram会将用户发送回您的应用程序(使用重定向URL值)以及可用于访 ...
  • 你必须添加另一个&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 ...
  • + 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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)