gpt4 book ai didi

django - 如何使用 Django Rest Framework 在其他表中插入外键?

转载 作者:行者123 更新时间:2023-12-04 00:28:14 27 4
gpt4 key购买 nike

我正在使用 Django REST 框架,我被困在 Inserting foreign-key data into another table 中。请引用以下场景:
如下面的代码所示,我想在 template 表中插入数据,并使用单个发布请求将外键关系插入到 template_owners 表中。
我尝试了一些解决方案,但没有达到预期效果,非常感谢您的帮助。

模型.py:

from django.db import models
from datetime import datetime
from django.utils import timezone

# Template model starts here.
class Template(models.Model):

Yes = "Yes"
No = 'No'

STATUS = [
(Yes, "Yes"),
(No, "No"),
]

class Meta:
db_table = "template"
ordering = ("date",)

uuid = models.CharField(max_length=64, null=False, primary_key=True)
name = models.CharField(max_length=255, null=False)
hypervisor = models.CharField(max_length=32, null=False)
download_url = models.CharField(max_length=255, null=False)
description = models.CharField(max_length=255, null=True)
metadata = models.CharField(max_length=255, null=True)
user = models.CharField(max_length=64, null=True)
command = models.CharField(max_length=32, null=True)
size = models.FloatField(null=False, blank=True, default=0)
is_default = models.CharField(max_length=3, choices=STATUS, default=Yes)
date_modified = models.DateTimeField(default=timezone.now)
date = models.DateTimeField(default=timezone.now)
owners = models.CharField(max_length=255, null=True)

def __str__(self):
return "Template [ uuid : {} ]" . format(self.uuid)

# Template model ends here.

# Template Owners model starts here.
class TemplateOwners(models.Model):

class Meta:
db_table = "template_owners"
ordering = ("date",)

template_uuid = models.ForeignKey(Template, on_delete=models.CASCADE, db_column="template_uuid")
gid = models.IntegerField(null=False, default=1)
uid = models.IntegerField(null=False, default=1)
date = models.DateTimeField(default=timezone.now)

# def __init__(self):
# self.fields['name'].error_messages = {'required': 'Please let us know what to call you!'}

def __str__(self):
return "Template Owner [ template_uuid : {} ]" . format(self.template_uuid)

# Template Owners model ends here.

Serializers.py:

from rest_framework import serializers
from template.models import Template, TemplateOwners

def trigger_validator(self, data):
if data["name"] == "":
raise serializers.ValidationError({"name": "Template name is required"})

return data

class TemplateOwnerSerializer(serializers.ModelSerializer):
class Meta:
model = TemplateOwners
fields = ("gid", "uid", "date", "template_uuid")

class TemplateSerializer(serializers.ModelSerializer):
owners = TemplateOwnerSerializer(many=True)

class Meta:
model = Template
fields = ("uuid", "name", "hypervisor", "download_url", "size", "date", "owners")

def create(self, validated_data):
owners_data = validated_data.pop('owners')
template = Template.objects.create(**validated_data)
for owner_data in owners_data:
TemplateOwners.objects.create(template=template, **owner_data)
return template

Views.py:

from django.http import Http404
from rest_framework.response import Response
from rest_framework import status

from template.models import Template
from template.serializers import TemplateSerializer
from template.serializers import TemplateOwnerSerializer
from rest_framework.permissions import IsAuthenticated
from rest_framework import generics

class TemplateList(generics.ListCreateAPIView):

permission_classes = (IsAuthenticated,)
queryset = Template.objects.all()
serializer_class = TemplateSerializer

def get(self, request, fromat=None):
templates = Template.objects.all()
serialized = TemplateSerializer(templates, many=True)
return Response(serialized.data)

def post(self, request, fromat=None):
serializer = TemplateSerializer(data=request.data)
if serializer.is_valid():
serializer.save()

#tempowner = TemplateOwnerSerializer(data=request.data)
#if tempowner.is_valid():
#tempowner.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class TemplateModify(generics.RetrieveUpdateDestroyAPIView):

permission_classes = (IsAuthenticated,)
serializer_class = TemplateSerializer

def get_object(self, pk):
try:
return Template.objects.get(pk=pk)
except Template.DoesNotExist:
raise Http404

def get(self, request, pk, fromat=None):
template = self.get_object(pk)
serialized = TemplateSerializer(template)
return Response(serialized.data)

def put(self, request, pk, fromat=None):
template = self.get_object(pk)
serializer = TemplateSerializer(template, data=request.DATA)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)

return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

def delete(self, request, pk, format=None):
template = self.get_object(pk)
template.delete()
return Response(status=status.HTTP_204_NO_CONTENT)

在 Django Rest Framework 中与外键相关的问题很少,但都与数据插入无关,它们主要关注数据获取,因此请在标记重复之前检查整个场景。

最佳答案

您在序列化程序和创建方面几乎没有问题,因此请相应地更改它们

class TemplateOwnerSerializer(serializers.ModelSerializer):
class Meta:
model = TemplateOwners
fields = ("gid", "uid", "date")

class TemplateSerializer(serializers.ModelSerializer):
owners_data = TemplateOwnerSerializer(many=True)

class Meta:
model = Template
fields = ("uuid", "name", "hypervisor", "download_url", "size", "date", "owners_data")

def create(self, validated_data):
owners_data = validated_data.pop('owners_data')
template = Template.objects.create(**validated_data)
for owner_data in owners_data:
TemplateOwners.objects.create(template_uuid=template, **owner_data)
return template

关于django - 如何使用 Django Rest Framework 在其他表中插入外键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54511557/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com