首页 \ 问答 \ 我如何将相关的字段包含在django rest框架中的模型序列化中?(How can I include related fields to the model serialization in django rest framework?)

我如何将相关的字段包含在django rest框架中的模型序列化中?(How can I include related fields to the model serialization in django rest framework?)

我有两个模型:

class User(models.Model):
  username = models.CharField()

class Post(models.Model):
  text = models.TextField()
  owner = models.ForeignKey(User)

使用这样的序列化器:

from rest_framework.serializers import ModelSerializer

class PostSerializer(ModelSerializer):
    owner = serializers.Field(source='owner.id')

    class Meta:
        model = Post
        fields = ['text', 'owner']

我通过所有者的ID获取所有帖子。 如何修改序列化程序以获取包含所有用户模型的拥有者的所有帖子?

我尝试从rest_framework.serializers导入ModelSerializer

class PostSerializer(ModelSerializer):
    owner = serializers.Field(source='owner')

    class Meta:
        model = Post
        fields = ['text', 'owner']

但只是用用户名替换id,而不是我期望的整个模型(因为用户的表示返回用户名字段)。


I have two models:

class User(models.Model):
  username = models.CharField()

and

class Post(models.Model):
  text = models.TextField()
  owner = models.ForeignKey(User)

Using such serializer:

from rest_framework.serializers import ModelSerializer

class PostSerializer(ModelSerializer):
    owner = serializers.Field(source='owner.id')

    class Meta:
        model = Post
        fields = ['text', 'owner']

I get all posts with owners' ids. How can I modify serializer to get all posts with owner fialed containing the whole user model?

I tryied from rest_framework.serializers import ModelSerializer

class PostSerializer(ModelSerializer):
    owner = serializers.Field(source='owner')

    class Meta:
        model = Post
        fields = ['text', 'owner']

but that only replaced id with username, not the whole model as I expected(because User's representation returns username field).


原文:https://stackoverflow.com/questions/23522084
更新时间:2022-03-18 09:03

最满意答案

我认为他们之间没有区别。

因为loadRequest是异步的,所以它不会阻塞主线程。


I don't think they have differences between them.

Because loadRequest is asynchronous, it won't block the main thread.

相关问答

更多
  • 请注意,我不信任“禁止JIT模块加载优化”选项,我在没有调试的情况下产生了进程,并在JIT运行后附加了我的调试器。 在单行更快的版本中,这是Main : SingleLineTest(); 00000000 push ebp 00000001 mov ebp,esp 00000003 call dword ptr ds:[0019380Ch] MultiLineTest(); 00000009 call ...
  • 不,它不会影响性能,因为INT类型的固定长度为4B 。 因此,无论你有1位数字还是10位数字,性能应该是相同的。 INT(N) N代表要显示的位数,换句话说, INT(1)与INT(10)尺寸相同, 更新,如果你加快ORDER BY部分,你可以在该列上添加一个INDEX 在某些情况下,MySQL可以使用索引来满足ORDER BY子句,而无需执行任何额外的排序。 只要所有未使用的索引部分和所有额外的ORDER BY列都是WHERE子句中的常量,即使ORDER BY与索引完全不匹配,也可以使用索引 DOCS N ...
  • on子句中的条件顺序不应影响性能。 为什么不? 在高级别上是SQL执行的三个步骤: 解析查询 构造并优化“可执行”代码 执行代码 第二级优化查询,并应考虑执行查询的不同方法。 连接条件是此优化的一部分 - 一次完成。 从理论上讲,无论join的顺序是什么都没关系,尽管在一个非常复杂的查询中,它可能很重要。 The order of conditions in the on clause should not affect performance. Why not? At a high level are t ...
  • 那行不通。 只需要两个WebView。 显示带有gif的那个,而另一个正在加载。 在页面加载开关可见性。 (View.GONE和View.VISIBLE)。 That will not do. Just take two WebViews. Display the one with the gif while the other is loading. On page loaded switch visibilities. (View.GONE and View.VISIBLE).
  • 我认为他们之间没有区别。 因为loadRequest是异步的,所以它不会阻塞主线程。 I don't think they have differences between them. Because loadRequest is asynchronous, it won't block the main thread.
  • 问题是在一个WebView实例上设置的这些设置是否会影响另一个WebView实例的浏览器 否。如果更改一个WebView的设置,其他WebView的设置将保持不变。 IE浏览器。 如果更改myWebView的设置,则SomeOtherWebview仍将具有原始设置。 activity.finish(),稍后在其他活动中实例化新的webView 您的新WebView将是一个新的,具有默认设置。 请注意,您应用中的所有WebViews共享: DOM(Html5)存储。 不知道如何清除这一点。 饼干。 要清除,请 ...
  • 桌上的所有页面或数据页面上有什么锁定方案? (您可以通过选择lockscheme('table_name')来找到。索引维护的(应用程序观察的)性能在数据页锁定方案中要好得多。 索引是有序的。 插入时间取决于维护该订单的成本。 如果您插入的行的索引值列单调递增,那么索引将会'增加'并且性能会很好(取决于多个并发更新的任何并发问题)。 索引树将不时需要重新平衡,但我认为这是一项快速操作。 如果插入的顺序与索引的顺序不同,那么该索引必须将条目插入“中间”,这很可能导致页面分割(通过设置填充区域来留下足够的空间而 ...
  • 我想你可以捕捉原始网页的图像,并在任何你想要的地方显示这个图像。 但是,通过这种方式,您无法触摸复制页面上的URL链接或HTML输入/按钮,因为它只是原始页面的屏幕截图。 更具体地说,您可以: 使用WebViewClassic.saveViewState()和loadViewState()来保存和显示网页的副本。 要么: 使用WebView.capturePicture()将导致android.graphics.Picture对象。 或者您也可以参考Android开源浏览器:Tab.capture()来了解 ...
  • 是的,它将检查第一个条件拳头,然后如果它是假的,它将检查以下条件。 但是,为了提高性能,我建议您使用更多矢量化代码: for i=find(condition1(1:10000)) statements1 end for i=find(condition2(1:10000)) statements2 end for i=find(condition3(1:10000)) statements3 end 例如condition2=@(i) (i>2 & i<10) Yes, it will ...
  • 我最终创建了一个新的透明活动,并在新活动中播放视频。 清单: 新Ontouch: public boolean onTouch(View v, MotionEven ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。