我正在使用django rest框架构建一个TODO应用程序。我有两个模型,即todolist和todoitem。我如何为特定的todolist设置一个新的todoitem?我尝试使用外键,但它没有成功,抱歉我的英语不好,有谁能帮助我
我的models.py
:
class todolist(models.Model):
user = models.ForeignKey(User,on_delete=models.CASCADE,default=None)
name = models.CharField(max_length=128)
creation_date = models.DateField(auto_now=True)
def __unicode__(self):
return self.name
class todoitem(models.Model):
description = models.CharField(max_length=150)
completed = models.BooleanField(default=False)
due_by = models.DateField(auto_now=True)
parent = models.ForeignKey(todolist,on_delete=models.CASCADE)
def _unicode__(self):
return self.description
我的classviews.py
:
class Todolist(LoginRequiredMixin,ListCreateAPIView):
serializer_class = listserializer
def perform_create(self, serializer):
serializer.save(user=self.request.user)
def get_queryset(self):
self.queryset = todolist.objects.filter(user=self.request.user)
return super(Todolist, self).get_queryset()
class Todolistdetail(LoginRequiredMixin,RetrieveUpdateDestroyAPIView):
def get_queryset(self):
self.queryset = todolist.objects.filter(user=self.request.user)
return super(Todolistdetail, self).get_queryset()
serializer_class = listserializer
class Todoitem(LoginRequiredMixin,ListCreateAPIView):
def perform_create(self, serializer):
temp = self.queryset(todolist.objects.filter(id=self.kwargs['pk']))
serializer.save(parent=temp)
def get_queryset(self):
temp = todolist.objects.filter(id=self.kwargs['pk'])
self.queryset = todoitem.objects.filter(parent=temp)
return super(Todoitem, self).get_queryset()
serializer_class = itemserializer
class Todoitemdetail(LoginRequiredMixin,RetrieveUpdateDestroyAPIView):
def get_queryset(self):
temp = todolist.objects.filter(id=self.kwargs['list_id'])
self.queryset = todoitem.objects.filter(parent=temp)
return super(Todoitemdetail, self).get_queryset()
serializer_class = itemserializer
我的serializers.py
:
class listserializer(serializers.ModelSerializer):
user = serializers.ReadOnlyField(source='user.username')
class Meta:
model = todolist
fields = '__all__'
class itemserializer(serializers.ModelSerializer):
parent = serializers.ReadOnlyField(source='todolist.id')
class Meta:
model = todoitem
fields = '__all__'
我的urls.py
:
from todoapp import classviews
app_name = "todoapp"
urlpatterns = [
url(r'^list/$',classviews.Todolist.as_view(),name="list_lists"),
url(r'^list/(?P<pk>[0-9]+)/$',classviews.Todolistdetail.as_view(),name="each_list"),
url(r'^list/(?P<pk>[0-9]+)/item/$',classviews.Todoitem.as_view(),name="list_items"),
url(r'^list/(?P<list_id>[0-9]+)/item/(?P<pk>[0-9]+)/$',classviews.Todoitemdetail.as_view(),name="each_item")
]
答案 0 :(得分:0)
视图:
from rest_framework.viewsets import ModelViewSet
class TodoitemViewSet(ModelViewSet):
queryset = Todoitem.objects.all()
serializer_class = itemserializer
permission_classes = (IsAuthenticated)
serializer.py
class itemserializer(serializers.ModelSerializer):
class Meta:
model=todoitem
fields='__all__'
URL:
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'todo_item', TodoitemViewSet)
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include(router.urls)),
]
jsut将数据{'parent':1 'description':'example',....}
发布到www.example.com/todo_item/
,父母是您的专业人士的身份。