manyToMany与django休息框架 [英] manyToMany with django rest framework

查看:120
本文介绍了manyToMany与django休息框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用django-rest-framework提供的默认CRUD操作。它与普通型号相当,但我的一个型号与另一个标签型号有很多关系。这是模型的代码

  class ActivityType(models.Model):
title = models.CharField(max_length = 200)
slug = models.CharField(max_length = 250,unique = True)

def __unicode __(self):
return self.slug

class Activity(models.Model):
owner = models.ForeignKey('auth.user')
title = models.CharField(max_length = 200)
slug = models.CharField(max_length = 250,unique = True)
description = models.TextField()
tags = models.ManyToManyField(ActivityType)
created = models.DateTimeField(auto_now_add = True,blank = True)

def __unicode __(self):
return self.slug

想知道是什么是最好的方法来集成DRF,如果可能的话,从头开始编写所有的CRUD操作。

解决方案

在您的serializers.py

 从休息_framework import serializers 
from rest_framework import generics

from models import Activity
from models import ActivityType

class ActivityTypeSerializer(serializers.ModelSerializer):

class Meta:
model = ActivityType
fields =('id','title','slug')

class ActivitySerializer(serializers.ModelSerializer)

tags = ActivityTypeSerializer(many = True,read_only = True)

class Meta:
model = Activity
fields =('id'所有者','标题','slug','description','tags','created')

在您的views.py

 从rest_framework导入视图

从序列化器导入ActivitySerializer
从序列化器导入ActivityTypeSerializer

从模型导入活动
从模型导入ActivityType

类ActivityViewSet(viewsets.ModelViewSet):
queryset = Activity.objects.all()
serializer_class = ActivitySerializer

class ActivityTypeViewSet(viewsets.ModelViewSet):
queryset = ActivityType.objects.all()
serializer_class = ActivityTypeSerializer

和您的urls.py


$来自rest_framework.urlpatterns的$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
从rest_framework导入视图,路由器

从your_app.views导入ActivityTypeViewSet
从your_app.views导入ActivityViewSet

路由器= routes.DefaultRouter()

路由器。注册(r'activitytypes,ActivityTypeViewSet)
router.register(r'activities',ActivityViewSet)

还要确保在文档中描述的restframework网址包含

  urlpatterns = patterns('',

#你的其他url

url(r'^ api / $',include('rest_framework.urls',namespace ='rest_framework')),
url(r'api / accounts /',include('rest_framework.urls',namespace ='rest_framework')),



I am currently using the default CRUD operations provided by django-rest-framework. It works well with normal models but one of my model has many-many relation with another tag model. Here is the code for models

class ActivityType(models.Model):
    title = models.CharField(max_length=200)
    slug = models.CharField(max_length=250,unique=True)

    def __unicode__(self):
        return self.slug        

class Activity(models.Model):
    owner = models.ForeignKey('auth.user')
    title = models.CharField(max_length=200)
    slug = models.CharField(max_length=250,unique=True)
    description = models.TextField()
    tags = models.ManyToManyField(ActivityType)
    created = models.DateTimeField(auto_now_add=True, blank=True)

    def __unicode__(self):
        return self.slug

What i want to know is what is the best method to integrate DRF with the same, if possible without writing all CRUD operations from scratch.

解决方案

In your serializers.py

from rest_framework import serializers
from rest_framework import generics

from models import Activity
from models import ActivityType

class ActivityTypeSerializer(serializers.ModelSerializer):

    class Meta:
        model = ActivityType
        fields = ('id', 'title', 'slug')

class ActivitySerializer(serializers.ModelSerializer):

    tags = ActivityTypeSerializer(many=True, read_only=True)

    class Meta:
        model = Activity
        fields = ('id', 'owner', 'title', 'slug', 'description', 'tags', 'created')

in your views.py

from rest_framework import viewsets

from serializers import ActivitySerializer
from serializers import ActivityTypeSerializer

from models import Activity
from models import ActivityType

class ActivityViewSet(viewsets.ModelViewSet):
    queryset = Activity.objects.all()
    serializer_class = ActivitySerializer

class ActivityTypeViewSet(viewsets.ModelViewSet):
    queryset = ActivityType.objects.all()
    serializer_class = ActivityTypeSerializer

and in your urls.py

from rest_framework.urlpatterns import format_suffix_patterns
from rest_framework import routers, serializers, viewsets
from rest_framework import generics
from rest_framework import viewsets, routers  

from your_app.views import ActivityTypeViewSet
from your_app.views import ActivityViewSet

router = routers.DefaultRouter()

router.register(r'activitytypes', ActivityTypeViewSet)  
router.register(r'activities', ActivityViewSet)

Also make sure the restframework urls are included as described in docs

 urlpatterns = patterns('',

 # your other urls 

     url(r'^api/$', include('rest_framework.urls', namespace='rest_framework')),
     url(r'api/accounts/', include('rest_framework.urls', namespace='rest_framework')),

 ) 

这篇关于manyToMany与django休息框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆