简体   繁体   English

Django内容类型:我如何获得一个对象?

[英]Django content-type : how do I get an object?

If I have a django content_type reference (the id of the model.class and the id of the object), what's the best way to get the actual object itself? 如果我有一个django content_type引用(model.class的id和对象的id),那么获取实际对象本身的最佳方法是什么?

Sounds trivial but I can't actually see an example anywhere. 听起来微不足道但我实际上无法在任何地方看到一个例子。

From memory, it is something like this: 从记忆中,它是这样的:

from django.contrib.contenttypes.models import ContentType
ct = ContentType.objects.get_for_id(content_type)
obj = ct.get_object_for_this_type(pk=object_id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在哪里定义我想在 Django API 中接收的 Content-Type? - Where do I define the Content-Type I want to receive in my Django API? 在Shopify App的Django HttpResponse对象中设置Content-Type - Setting Content-Type in Django HttpResponse object for Shopify App 如何获取Django响应对象的内容长度? - How do I get the content length of a Django response object? 如何使用django.test.Client发出没有Content-Type标头的HTTP请求? - How can I make an HTTP request with no Content-Type header using django.test.Client? 当content-type为“application / xml”时,如何使用httplib发布非ASCII字符 - How do I post non-ASCII characters using httplib when content-type is “application/xml” 使用带有 S3boto 后端的 django-storages 保存到 S3 时如何设置“Content-Type”? - How do you set "Content-Type" when saving to S3 using django-storages with S3boto backend? 用于text / xsl的Django Content-Type - Django Content-Type for text/xsl django DJA,带有浮动设置内容类型 - django DJA with swagger setting content-type 如何处理 Django HTTP.Request、请求内容类型、查询参数 - How to handle with Django HTTP.Request, request content-type, query parameters 内容类型和表示 - Content-Type and Representations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM