我已经阅读了这个问题如何在 Django Rest Framework JSON API 中生成 JSON-API 数据属性与结果属性?但是接受的答案对我不起作用,而且我的情况未涵盖其中。
我正在使用 Django Rest Framework (3.5.3) 来提供 API 端点。该终点基于
class FruitTestReadOnlyViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Fruit.objects.all()
serializer_class = FruitSerializer
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)
Run Code Online (Sandbox Code Playgroud)
从那里返回的数据很好,只是它具有“结果”属性而不是“数据”属性。
我上面提到的问题建议你这样做
class FruitTestReadOnlyViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Fruit.objects.all()
serializer_class = FruitSerializer
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)
renderer_classes = (JSONRenderer,)
parser_classes = (JSONParser,)
Run Code Online (Sandbox Code Playgroud)
但这对我来说没有区别,事实上我已经有了全局设置,我相信它涵盖了渲染和解析器方面
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
),
'PAGE_SIZE': 10,
'EXCEPTION_HANDLER': 'rest_framework_json_api.exceptions.exception_handler',
'DEFAULT_PAGINATION_CLASS':
'rest_framework_json_api.pagination.PageNumberPagination',
'DEFAULT_PARSER_CLASSES': (
'rest_framework_json_api.parsers.JSONParser',
'rest_framework.parsers.FormParser',
'rest_framework.parsers.MultiPartParser'
),
'DEFAULT_RENDERER_CLASSES': ( …Run Code Online (Sandbox Code Playgroud) 我在Django中有两个模型:
class Thread(models.Model):
entity = models.ForeignKey(Entity, null=True, blank=True)
...
class ThreadMessage(models.Model):
thread = models.ForeignKey(Thread, related_name='messages')
author = models.ForeignKey(User)
...
Run Code Online (Sandbox Code Playgroud)
现在,客户端想要创建一个包含第一条消息的新线程.它首先做一个POST /threads创建一个新线程并找出它id然后POST /messages在thread字段中传递找到的id .
我想如果在Ember的一个请求中完成所有这些是合理的和可能的,例如:
POST /messages
{"message": {"text": "text", ...},
"thread": {"entity": 1}}
Run Code Online (Sandbox Code Playgroud)
响应将是:
{"message": {"text": "text", "id": 5678, "thread": 1234, ...},
"thread": {"entitity": 1, "id": 1234, ...}}
Run Code Online (Sandbox Code Playgroud) 出于某种原因"开箱即用",Wordpress JSON API无法在Nginx上运行.我在nginx conf中尝试了几种重定向方案.我唯一能做的就是?json.但是,这不适用于身份验证和注册.
作为一个FYI,我正在开发一个cordova应用程序并尝试使用WP JSON API进行WP后端.
技术背景:rails 4.2.2,active_model_serializers 0.10.0.rc2 给定一个购物车和一个产品列表,当我将产品添加到购物车时,我希望得到响应:
{
"data": {
"id": "575",
"type": "carts",
"attributes": {
"name": "cart 1"
},
"relationships": {
"cart_products": {
"data": [
{
"type": "cart_products",
"id": "32",
"attributes": {
"product_id": 456
}
}
]
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,目前的回应是
{
"data": {
"id": "575",
"type": "carts",
"attributes": {
"name": "cart 1"
},
"relationships": {
"cart_products": {
"data": [
{
"type": "cart_products",
"id": "32",
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
} }
有没有办法渲染关系属性?
我的drf后端有这个模型:
class Product:
price_range = ...
Run Code Online (Sandbox Code Playgroud)
我在JSONApi序列化程序中使用EmberData。我刚刚发现JSON API需要使用反斜线属性。所以我需要告诉Drf:
JSON_API_FORMAT_KEYS = 'dasherize'
Run Code Online (Sandbox Code Playgroud)
并且该属性被序列化为JSON:
price-range
Run Code Online (Sandbox Code Playgroud)
然后,EmberData进行舞蹈,然后得到Ember模型属性:
import DS from 'ember-data';
export default DS.Model.extend({
...
priceRange: DS.attr('number'),
...
});
Run Code Online (Sandbox Code Playgroud)
(如果我没记错的话,那是旧的RESTSerializer期望)priceRangeJSON
因此,我们从price_range-> price-range-> priceRange(如果您问我,这太疯狂了)。我通过反复试验发现了所有这些。对于drf,此处记录了相应的设置。
这哪里是记录了JSONApi,EmberData和Ember?我想确保我已经真正理解了这一点,并且关系也是如此。相关的drf配置设置为:
JSON_API_FORMAT_RELATION_KEYS = 'dasherize'
Run Code Online (Sandbox Code Playgroud) 在哪里以及如何公开API端点的架构是否有标准?
例如,假设以下API端点可用:
api/companies/
api/companies/{id}/employees/
Run Code Online (Sandbox Code Playgroud)
应该在哪里公开公司和员工资源的架构?
api/company-schema.json和api/employee-schema.json?
api/schemas/company.json和api/schemas/employee.json?
我正在使用Django Rest Framework JSON API创建REST API。我正在尝试非常简单地包括一个相关资源(二级关系),但是Django不断响应该错误:
This endpoint does not support the include parameter for path...
Run Code Online (Sandbox Code Playgroud)
结构是这样的:
# models:
class Household(models.Model):
...
class HouseholdMember(models.Model):
household = models.ForeignKey(Household)
...
class Subscription(models.Model):
subscriber = models.ForeignKey(HouseholdMember)
...
# serializers
from rest_framework_json_api import serializers
class SubscriptionSerializer(serializers.ModelSerializer):
class Meta:
model = Subscription
Run Code Online (Sandbox Code Playgroud)
我希望能够发出这样的请求:http://example.com/api/subscriptions?include=subscriber.household能够按家庭对订阅进行分组。但是,我根本找不到解决方法。我知道我需要一起玩,ResourceRelatedField但是我缺少新手或某些东西,无法理解它的工作原理。有什么帮助吗?
我需要在retrofit2中使用jsonapi.我尝试使用moshi-jsonapi,但我不能使用moshi ConverterFactory.
TokenModel.java
@JsonApi(type = "tokens")
public class TokenModel extends Resource {
@Json(name = "cell_phone")
public String cellPhone;
}
Run Code Online (Sandbox Code Playgroud)
TestService.java:
public interface TestService {
@POST("token")
Call<TokenModel> newOtp(@Body TokenModel tokenModel);
}
Run Code Online (Sandbox Code Playgroud)
TestProvider.java:
public class TestProvider {
private TestService testService;
public TestProvider() {
OkHttpClient httpClient = new OkHttpClient();
Retrofit refRetrofit = new Retrofit.Builder()
.baseUrl(ClientConfigs.BASE_URL)
.client(httpClient)
.addConverterFactory(MoshiConverterFactory.create())
// .addConverterFactory(????????????????????????????)
.build();
testService = refRetrofit.create(TestService.class);
}
publicTestService getTestService() {
return testService;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我使用MoshiConverterFactorymake错误Unable to create …
任何人都可以解释使用json-rpc优于json-api的优势,反之亦然?第一种和第二种格式是基于JSON的,但我应该使用哪一种,哪一种是另一种?
在我假设的应用程序中的某个时刻,为了效率,我想在单个请求中创建多个不同类型的相关实体。在下面的示例中,我以包含有关新用户及其相关头像的数据的方式序列化请求。
// POST /api/users
{
data: {
attributes: { ... },
type: 'user',
relationships: {
avatar: {
data: {
attributes: { ... }
type: 'avatar',
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,在 JSONAPI 中这样做的正确/推荐方法(如果有的话)是什么?