首页
\
问答
\
Django Rest框架 - 在序列化程序中获取相关模型字段(Django Rest Framework - Get related model field in serializer)
Django Rest框架 - 在序列化程序中获取相关模型字段(Django Rest Framework - Get related model field in serializer)
我正在尝试从Django Rest框架返回一个HttpResponse,包括来自2个链接模型的数据。 模型是:
class Wine(models.Model): color = models.CharField(max_length=100, blank=True) country = models.CharField(max_length=100, blank=True) region = models.CharField(max_length=100, blank=True) appellation = models.CharField(max_length=100, blank=True) class Bottle(models.Model): wine = models.ForeignKey(Wine, null=False) user = models.ForeignKey(User, null=False, related_name='bottles')
我想要一个瓶子模型的序列化器,其中包括相关葡萄酒的信息。
我试过了:
class BottleSerializer(serializers.HyperlinkedModelSerializer): wine = serializers.RelatedField(source='wine') class Meta: model = Bottle fields = ('url', 'wine.color', 'wine.country', 'user', 'date_rated', 'rating', 'comment', 'get_more')
这不行。
有什么想法我该怎么做?
谢谢 :)
I'm trying to return a HttpResponse from Django Rest Framework including data from 2 linked models. The models are:
class Wine(models.Model): color = models.CharField(max_length=100, blank=True) country = models.CharField(max_length=100, blank=True) region = models.CharField(max_length=100, blank=True) appellation = models.CharField(max_length=100, blank=True) class Bottle(models.Model): wine = models.ForeignKey(Wine, null=False) user = models.ForeignKey(User, null=False, related_name='bottles')
I'd like to have a serializer for the Bottle model which includes information from the related Wine.
I tried:
class BottleSerializer(serializers.HyperlinkedModelSerializer): wine = serializers.RelatedField(source='wine') class Meta: model = Bottle fields = ('url', 'wine.color', 'wine.country', 'user', 'date_rated', 'rating', 'comment', 'get_more')
which doesn't work.
Any ideas how I could do that?
Thanks :)
原文:https://stackoverflow.com/questions/20633313
更新时间:2023-07-13 15:07
最满意答案
Declare @Transaction table (UserID int,TransDate Date,UserBalance int) Insert into @Transaction Select 33782,'2016-05-13',233 Insert into @Transaction Select 33783,'2016-05-13',143 Insert into @Transaction Select 30070,'2016-05-20',572 Insert into @Transaction Select 30071,'2016-05-20',888 Declare @Calendar table (UserID int,BalanceDate date) Insert into @Calendar Select 33782,'2016-05-13' Insert into @Calendar Select 33783,'2016-05-13' Insert into @Calendar Select 30070,'2016-05-13' Insert into @Calendar Select 30071,'2016-05-13' Insert into @Calendar Select 33782,'2016-05-20' Insert into @Calendar Select 33783,'2016-05-20' Insert into @Calendar Select 30070,'2016-05-20' Insert into @Calendar Select 30071,'2016-05-20' Select A.*,UserBalance=isnull((Select top 1 UserBalance From @Transaction Where UserID=A.UserID and TransDate<=A.BalanceDate Order by TransDate Desc),0) From ( Select UserID,balanceDate From (Select Distinct UserID from @Calendar) U Join (Select Distinct balanceDate from @Calendar) C on 1=1 ) A Order by 2,1 Desc
返回
UserID balanceDate UserBalance 33783 2016-05-13 143 33782 2016-05-13 233 30071 2016-05-13 0 30070 2016-05-13 0 33783 2016-05-20 143 33782 2016-05-20 233 30071 2016-05-20 888 30070 2016-05-20 572
Declare @Transaction table (UserID int,TransDate Date,UserBalance int) Insert into @Transaction Select 33782,'2016-05-13',233 Insert into @Transaction Select 33783,'2016-05-13',143 Insert into @Transaction Select 30070,'2016-05-20',572 Insert into @Transaction Select 30071,'2016-05-20',888 Declare @Calendar table (UserID int,BalanceDate date) Insert into @Calendar Select 33782,'2016-05-13' Insert into @Calendar Select 33783,'2016-05-13' Insert into @Calendar Select 30070,'2016-05-13' Insert into @Calendar Select 30071,'2016-05-13' Insert into @Calendar Select 33782,'2016-05-20' Insert into @Calendar Select 33783,'2016-05-20' Insert into @Calendar Select 30070,'2016-05-20' Insert into @Calendar Select 30071,'2016-05-20' Select A.*,UserBalance=isnull((Select top 1 UserBalance From @Transaction Where UserID=A.UserID and TransDate<=A.BalanceDate Order by TransDate Desc),0) From ( Select UserID,balanceDate From (Select Distinct UserID from @Calendar) U Join (Select Distinct balanceDate from @Calendar) C on 1=1 ) A Order by 2,1 Desc
Returns
UserID balanceDate UserBalance 33783 2016-05-13 143 33782 2016-05-13 233 30071 2016-05-13 0 30070 2016-05-13 0 33783 2016-05-20 143 33782 2016-05-20 233 30071 2016-05-20 888 30070 2016-05-20 572
相关问答
更多-
就像其他人所说的那样,要获得要显示的点之间的真实时差,必须使用散点图而不是Excel线图。 要在中间日期显示余额的真实状态(即没有倾斜线),您需要对数据进行一些转换。 它相当于创建两个新的数据列,每个余额有重复点:一个在帐户余额达到该级别的日期,另一个在余额离开该级别的日期。 我建议如下:在第一个单元格中填充您的Balance列(我在E2处开始我的表)输入 =B2 其中B2是原始表中第一个余额的地址。 在下一个单元格中输入 =IF(E2<>E1,E2,OFFSET(B$2,COUNT(E$2:E2)/2, ...
-
正如评论中所提到的,您的问题是您的代码仅在有一个事务时才有效。 下面是您的代码在插入中处理多行的示例。 If Not Exists (Select 1 From Deleted) -- Insert Trigger Update CustomerMaster Set AccountBalance = AccountBalance + ( Select Sum(I.Amount) From Inserted I Where ...
-
SQL帐户余额表(SQL Account Balance Table)[2023-08-03]
Declare @Transaction table (UserID int,TransDate Date,UserBalance int) Insert into @Transaction Select 33782,'2016-05-13',233 Insert into @Transaction Select 33783,'2016-05-13',143 Insert into @Transaction Select 30070,'2016-05-20',572 Insert into @Transac ... -
一个从未被优雅解决的古老问题。 我所使用的所有银行包都与账户实体存放余额。 从运动史上算起来是不可想象的。 正确的方法是: 移动表具有每个帐户的“期初余额”交易。 您需要在几年的时间内,您需要将旧移动从主动移动表移动到历史记录表。 帐户实体有一个余额字段 移动表上有一个触发器,用于更新贷方和借记帐户的帐户余额。 显然,它有承诺控制。 如果你不能有一个触发器,那么需要有一个独特的模块来编写承诺控制下的动作 您有一个“安全网”程序,您可以离线运行,这将重新计算所有的余额和显示(并可选地纠正)错误的余额。 这对测 ...
-
平均每月帐户余额(Average Monthly Balance of Accounts)[2023-07-29]
从表中选择AVG(余额),其中branch_code =“123”分组由Account_Num 我猜..这应该工作.. :) Select AVG(balance) from table where branch_code="123" group by Account_Num I guess.. this should work.. :) -
如果您将提供表格和一些示例数据的描述,那么提供更正确的查询会更容易。 但是,我认为这个查询可以工作 - 它将返回与BALANCE最接近的给定日期行: SELECT * FROM DATES d, BALANCE b WHERE d.date >= b.date AND b.date >= ALL (SELECT b1.date FROM BALANCE b1 WHERE b1.date <= d.date ...
-
原子计数器的文档很清楚,在我看来,对你的用例来说是不安全的。 您正在解决的问题很常见,AWS建议在这种情况下使用乐观锁定。 请参阅以下AWS文档, http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.OptimisticLocking.html It appears that this concept is workable, from a AWS staff reply Often applicati ...
-
获取Paypal账户的余额(Get balance of Paypal account)[2022-09-01]
以下是您可以使用的示例助手类:账户余额系统与条纹(Account balance system with Stripe)[2022-10-22]
你会按照这个教程。 上周我将它集成到我的购物车功能中。 它很容易集成...有乐趣:) http://justlaravel.com/integrate-stripe-payment-gateway-laravel/ You will follow this tutorial. I am integrated it in my cart functionality previous week. Its very easy to integrate ...have Fun :) http://justlarav ...考虑到余额变化将由费用记录变更触发,您可以覆盖在费用模型上保存。 然后可以在auto中维护balance table。 导入日期时间 class Expense(models.Model): date = models.DateTimeField(default=timezone.now) spender = models.ForeignKey('auth.User') description = models.CharField(max_length=200) catego ...相关文章
更多- 传说中的WeixinJSBridge和微信rest接口
- vicalloy的我与django
- Django and full-text search
- Django资源汇总(转)
- Django下载及安装
- 在django中加入搜索引擎
- IOS-Social.framework
- 【z】Storm - the world's best IDE framework for .NET
- 微信开发python+django两个月的成功经历,django是个好框架!
- django-haystack+solr实现搜索
最新问答
更多- h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
- 创建一个功能(Create a function)
- 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
- PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
- Qt冻结循环的原因?(Qt freezing cause of the loop?)
- TableView重复youtube-api结果(TableView Repeating youtube-api result)
- 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
- SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
- 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
- 以编程方式创建UITableView(Creating a UITableView Programmatically)
- 如何打破按钮上的生命周期循环(How to break do-while loop on button)
- C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
- 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
- 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
- Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
- 设置kotlin扩展名(Setting a kotlin extension)
- 如何并排放置两个元件?(How to position two elements side by side?)
- 如何在vim中启用python3?(How to enable python3 in vim?)
- 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
- 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
- Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
- dedecms如何安装?
- 在哪儿学计算机最好?
- 学php哪个的书 最好,本人菜鸟
- 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
- 如何覆盖错误堆栈getter(How to override Error stack getter)
- 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
- USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
- 电脑高中毕业学习去哪里培训
- 正则表达式验证SMTP响应(Regex to validate SMTP Responses)