gpt4 book ai didi

python - def form_valid 用于 Django Rest 框架

转载 作者:行者123 更新时间:2023-12-01 08:50:51 27 4
gpt4 key购买 nike

我是 Django Rest Framework 的新手。我一直在使用常规 Django,我想要类似于 Django 的 CreateView 的模型。

简介: super 用户是唯一的用户。所有患者属于用户,胚胎属于患者。一名患者可以拥有多个胚胎。以下是我的模型

class Patients(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
first_name = models.CharField(max_length=25)
last_name = models.CharField(max_length=35)
email = models.EmailField(unique=True)


class Embryo(models.Model):
patient = models.ForeignKey(Patients, on_delete=models.CASCADE)
code_name = models.CharField(max_length=100)
karyotype = models.CharField(max_length=100)
GENDER_CHOICES = (
("M", "Male"),
("F", "Female"),
)
sex = models.CharField(max_length=1, choices=GENDER_CHOICES)

我正在尝试为我的模型创建 View 。以下是我目前所掌握的内容

class PatientsApiView(viewsets.ModelViewSet):
"""Handles Creating, reading and updating Patients"""

serializer_class = serializers.PatientsSerializer
queryset = Patients.objects.all()
authentication_classes = (TokenAuthentication,)
filter_backends = (filters.SearchFilter,)
search_fields = ("first_name", "last_name", "phone", "email",)

def form_valid(self, form, *args, **kwargs):
self.object = form.save(commit=False)
self.object.user = self.request.user
self.object.save()
return super().form_valid(form)

"""How can I get the above with Django Rest Framework. Can I do:
def __getattr__(self, user):
return self.request.user
"""

下面是 EmbroApiView 的 View 集

class EmbroApiView(viewsets.ModelViewSet):
"""Handles Creating, reading and updating Patients"""

serializer_class = serializers.EmbryoSerializer
queryset = Embryo.objects.all()
authentication_classes = (TokenAuthentication,)
filter_backends = (filters.SearchFilter,)
search_fields = ("code_name", "karyotype", "sex", "down_syndrome",)

def form_valid(self, form, *args, **kwargs):
self.object = form.save(commit=False)
pk = self.kwargs.get('pk')
self.object.patient = get_object_or_404(Patient, pk=pk)
self.object.save()
return super().form_valid(form)

"""How can I get the above with Django Rest Framework. Can I do:
def __getattr__(self, patient):
return get_object_or_404(Patients, pk=self.patient.pk)
"""

根据@Daniel Roseman 解决方案。我这样做正确吗?

class PatientsApiView(viewsets.ModelViewSet):
"""Handles Creating, reading and updating Patients"""

serializer_class = serializers.PatientsSerializer
queryset = Patients.objects.all()
authentication_classes = (TokenAuthentication,)
filter_backends = (filters.SearchFilter,)
permission_classes = (IsAuthenticated,)
search_fields = ("first_name", "last_name", "phone", "email",)

def perform_create(self, serializer):
serializer.save(user=self.request.user)


class EmbroApiView(viewsets.ModelViewSet):
"""Handles Creating, reading and updating Patients"""

serializer_class = serializers.EmbryoSerializer
queryset = Embryo.objects.all()
authentication_classes = (TokenAuthentication,)
filter_backends = (filters.SearchFilter,)
permission_classes = (IsAuthenticated,)
search_fields = ("code_name", "karyotype", "sex", "down_syndrome",)

def perform_create(self, serializer):
serializer.save(patient_id=self.kwargs['pk'])

最佳答案

我不知道你为什么在评论中提到__getattr__,这与此无关。

创建和更新时执行额外操作的 Hook 称为 perform_createperform_update。如the docs显示,你可以这样做:

def perform_create(self, serializer)
serializer.save(user=self.request.user)

等等

关于python - def form_valid 用于 Django Rest 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53139295/

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