0
我是django-rest-framework的新手。我正在構建一個員工調度應用程序,其中我有一個使用drf和前端構建的REST API。下面是我的模型之一,它是corrsponding序列化器和視圖集。在django-rest-framework中添加警告消息
型號:
class Eventdetail(models.Model):
event = models.ForeignKey(Event, models.DO_NOTHING, blank=True, null=True)
start = models.DateTimeField(blank=True, null=True)
end = models.DateTimeField(blank=True, null=True)
employee = models.ForeignKey(Employee, models.DO_NOTHING, blank=True, null=True)
location = models.ForeignKey(Location, models.DO_NOTHING, blank=True, null=True)
is_daily_detail = models.BooleanField
def __str__(self):
return self.event
串行:
class LocationTrackSerializer(serializers.ModelSerializer):
def __init__(self, *args, **kwargs):
many = kwargs.pop('many', True)
super(LocationTrackSerializer, self).__init__(many=many, *args, **kwargs)
location = serializers.SlugRelatedField(slug_field='location_name', queryset=Location.objects.all())
location_color = serializers.CharField(source='location.location_color', read_only=True)
class Meta:
model = Eventdetail
fields = ('id','employee','location','location_color','start','end')
視圖集:
class LocationTrackViewSet(viewsets.ModelViewSet):
queryset = Eventdetail.objects.all()
serializer_class = LocationTrackSerializer
def create(self, request, *args, **kwargs):
self.user = request.user
listOfThings = request.data['events']
serializer = self.get_serializer(data=listOfThings, many=True)
if serializer.is_valid():
serializer.save()
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED,
headers=headers)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
正如你所看到的,這暴露了所有員工的事件細節。現在,當發佈新事件時,我希望能夠查找發佈事件的開始時間和結束時間是否與現有事件重疊,並在創建後發出重疊事件信息的警告消息。我仍然希望允許保存,但只能在保存後返回警告。我正在想辦法做到這一點。我研究瞭如何創建驗證器,但我不確定是否應該這樣做。任何幫助表示讚賞!謝謝。
你是什麼意思的警告?其他數據以及串行器數據,是嗎? –
@SachinKukreja對不起,是的,這就是我的意思。如果新發布的事件與現有事件具有重疊日期,則表示「與現有事件重疊」的消息字段 – ash