假设我有这样的模型: class A(models.Model):
...some fields here
class B(models.Model):
...some fields here
a = models.ForeignKey(A, on_delete=CASCADE)
class C(models.Model):
...some fields here
b = models.ForeignKey(B, on_delete=CASCADE)
... 我希望我的API端点返回如下所示的内容 {
...some fields here
class AnnotationSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Annotation
class ImageSerializer(serializers.HyperlinkedModelSerializer):
annotations = AnnotationSerializer(many=True, required=False)
class Meta:
depth = 1
model = Image
作为一个新手Django开发人员,我正在尝试为一个移动应用程序构建一个RESTful API。我已经接管了一个现有的项目,以前的开发人员使用了Django REST框架。超酷的包,很容易使用到目前为止。除了一件事..。
当我想要创建新的资源时会出现这个问题,这些资源碰巧有嵌套的序列化程序。我不太擅长用文字解释软件问题,下面是我的案例的简化版本:
class UserSerializer(serializers.ModelSerializer):
company = CompanySerializer()
# other props and functions are unrel
请有人告诉我嵌套序列化程序使用它的好处是什么--非分离的api (换句话说,一个api与未分离的资源相同)?
FYI,我试图为创建和更新方法制作3个嵌套的序列化程序。许多部分被省略了,但我的图片如下。
# models.py
class Foo(Model):
title = CharField(...)
class Bar(Model):
foo = ForeignKey("Foo", ...)
class Bar2(Model):
bar = ForeignKey("Bar", ...)
# serializers.py
clas
有没有一个很好的创建API的例子,我可以在其中创建一个项目并向其中添加多个任务? class Project(models.Model):
name = models.CharField(max_length=30)
class Task(models.Model):
priority = models.CharField(max_length=30)
todo = models.CharField(max_length=30)
project = models.ForeignKey(Project, on_delete=models.CASCADE)
我对goggles Gson库有个问题。请看下面的代码:
public abstract class Main {
public String foo = "foo";
public List<String> bar = Arrays.asList( "foo", "bar" );
@Override
public String toString( ) {
Gson gson = new Gson( );
return gson.toJson( this );
(我正在使用最新的django/python/DRF)
我的API端点是内部应用程序。他们不匹配1比1与我在系统中的模型。我看到了两种将视图用于API的技术。
第一种方法使用方法,就像我使用的那样:
@api_view(['GET', 'POST'])
@authentication_classes([JSONWebTokenAuthentication])
def myApiEndPoint(request):
"""
This text is the description for this API.
"
Get方法在可浏览的api端点中工作,但当我尝试通过浏览器使用端点进行post时,它会触发以下错误:(我的序列化程序是嵌套的)
这是我的serializers.py,它是嵌套的serilizers
from rest_framework import serializers
from . models import Author, Article, Category, Organization
class OrganizationSerializer(serializers.ModelSerializer):
class Meta:
model = Org
我正在使用Django REST Framework和django-rest-swagger库来构建API端点。我想按自定义属性而不是URL对一些API URL进行分组。
例如,我有API端点,并希望按函数对它们进行分组:
# task list management
GET /api/tasks/known - get known tasks list with their parameters
GET /api/tasks - get last tasks list with their statuses
# Tasks by ID management
GET /api/tas
日安!
我只想问一下如何为ajax调用创建JSONAPISerializer?根据我在文档上的理解。我必须先创建一个模型,然后才能创建JSONAPISerializer。但是我需要调用一个自定义终结点,该终结点没有被列为我的模型。
我的目标是对来自我的端点的所有侧载数据进行pushPayload。但问题是:
{
"data":[
{
"type":"promotions-verify", <----- it's not an actual model
"attributes"
假设我有以下类结构:
public abstract class BaseModel
{
public string Name {get;set;}
}
public class ExtendedOne : BaseModel
{
prop string Prop1 {get;set;}
}
public class ExtendedTwo : BaseModel
{
prop string AnotherProp {get;set;}
}
然后我有了以下Web端点:
[Route("api/Test")
public IHttpActionResu