[英]How can I create API with Django and API REST Framework but based on existing models?
I have a project with Django and i need to create API REST based on existing models of the project. 我有一个与Django有关的项目,我需要根据该项目的现有模型创建API REST。 I mean, is it possible only import the models in the api/v1/models.py ? 我的意思是,是否只能将模型导入api / v1 / models.py中? And i need to use nested serializers but i don't understsand the documentation very well. 而且我需要使用嵌套的序列化器,但是我对文档的了解不是很好。
I have a project "hotel" and in the project i have various aplications for example "booking", "panel", "contact" and a lot of more. 我有一个项目“ hotel”,在项目中我有各种用途,例如“ booking”,“ panel”,“ contact”等等。 So, every aplication has models, views, urls. 因此,每个应用程序都有模型,视图,URL。
I need to create API REST based on every models. 我需要基于每个模型创建API REST。 Now i have created a new aplication with rest-framework named "api" and in the models i only import the models of the other aplications. 现在,我使用名为“ api”的其余框架创建了一个新的应用程序,并且在模型中,我仅导入其他应用程序的模型。 Basically this is the root of my project. 基本上,这是我项目的基础。
hotel/
booking/
__init__.py
views.py
models.py
forms.py
urls.py
panel
contact
api/ (with rest-framework)
__init__.py
models.py
serializers.py
urls.py
views.py
Actually in the api this is the structure: 实际上在api中,这是结构:
# models.py
from booking.models import ReserveClient, CodeClient, CodeHour
# serializers.py
from rest_framework import serializers
from api.models import CodeClient, CodeHour
class CodeHourSerializer(serializers.ModelSerializer):
class Meta:
model = CodeHour
fields = (
'hours',
'min_hours',
)
class CodeClientSerializer(serializers.ModelSerializer):
class Meta:
model = CodeClient
fields = (
'id',
'code',
'date',
'max_use',
'unique_for_user',
'new_client',
'message',
'comment',
)
# views.py
import django_filters
from api.models import CodeClient, CodeHour
from api.serializers import CodeClientSerializer, CodeHourSerializer
class CodeClientFilter(django_filters.FilterSet):
class Meta:
model = CodeClient
fields = ['code']
class CodeClientList(mixins.ListModelMixin,
mixins.CreateModelMixin,
generics.GenericAPIView):
queryset = CodeClient.objects.all()
serializer_class = CodeClientSerializer
filter_backends = (filters.DjangoFilterBackend,)
filter_class = CodeClientFilter
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
Ok, now i want to generate a view with both serializers, because i need response JSON like this: 好的,现在我想用两个序列化器生成一个视图,因为我需要这样的响应JSON:
[
{
id: 4,
code: "3hours",
date: null,
max_use: 0,
unique_for_user: true,
new_client: true,
menssage: "",
comment: "",
hours: "", (CodeHourSerializer)
min_hours: "", (CodeHourSerializer)
}
]
Hopefully with the code will be easier to understand. 希望代码会更容易理解。
Thanks. 谢谢。
Gustavo. 古斯塔沃。
I need to create API REST base on models of the project 我需要根据项目模型创建API REST
DRF provides a ModelViewSet
just for that, all you do is tie it to one of your models DRF为此提供了一个ModelViewSet
,您所要做的就是将它绑定到您的一个模型上
class MyModelViewSet(viewsets.ModelViewSet):
"""
A viewset for viewing and editing user instances.
"""
serializer_class = MyModelSerializer
queryset = MyModel.objects.all()
http://www.django-rest-framework.org/api-guide/viewsets/#modelviewset http://www.django-rest-framework.org/api-guide/viewsets/#modelviewset
I need to use nested serializers and for your serializer you can do: 我需要使用嵌套的序列化程序,对于您的序列化程序,您可以执行以下操作:
class MyModelSerializer(serializers.ModelSerializer):
nested_field = MyOtherModelSerializer()
class Meta:
model = MyModel
fields = ('field1', 'field2', 'field3')
http://www.django-rest-framework.org/api-guide/serializers/#specifying-nested-serialization http://www.django-rest-framework.org/api-guide/serializers/#specifying-nested-serialization
Not sure your model looks like, but I think you just need to add SerializerMethodField on your CodeClientSerializer. 不确定您的模型是什么样,但是我认为您只需要在CodeClientSerializer上添加SerializerMethodField。
class CodeClientSerializer(serializers.ModelSerializer):
hours = serializers.SerializerMethodField()
min_hours = serializers.SerializerMethodField()
class Meta:
model = CodeClient
fields = (
...
'hours',
'min_hours',
)
def get_hours(self, obj):
return HOURS # set your value here
def get_min_hours(self, obj):
return MIN_HOURS # set your value here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.