Django rest框架,创建后更新对象

 收藏

我有一个采用以下模型的DRF API:


class Points(models.Model):
    mission_name = models.CharField(name='MissionName',
                                    unique=True,
                                    max_length=255,
                                    blank=False,
                                    help_text="Enter the mission's name"
                                    )
    # Some irrlevant feid

    url = models.URLField(help_text='Leave Empty!', default=" ")

    date_added = models.DateTimeField(default=timezone.now)

    class Meta:
        get_latest_by = 'date_added'

它是序列化器:

from rest_framework.serializers import HyperlinkedModelSerializer

from .models import Points


class PointsSerializer(HyperlinkedModelSerializer):

    class Meta:
        model = Points
        fields = (
            'id', 'MissionName', 'GDT1Latitude', 'GDT1Longitude',
            'UavLatitude', 'UavLongitude', 'UavElevation', 'Area',
            'url', 'date_added'
        )

和视图:

class PointsViewSet(ModelViewSet):
    # Return all order by id, reversed.
    queryset = Points.objects.all().order_by('-id')
    serializer_class = PointsSerializer
    data = queryset[0]
    serialized_data = PointsSerializer(data, many=False)
    points = list(serialized_data.data.values())

    def retrieve(self, request, *args, **kwargs):
        print(self.data)
        mission_name = self.points[1]

        assign_gdt = GeoPoint(lat=self.points[2], long=self.points[3])
        gdt1 = [assign_gdt.get_lat(), assign_gdt.get_long()]

        assign_uav = GeoPoint(lat=self.points[4], long=self.points[5], elevation=self.points[6])
        uav = [assign_uav.get_lat(), assign_uav.get_long(), assign_uav.get_elevation()]

        area_name = f"'{self.points[-2]}'"

        main = MainApp.run(gdt1=gdt1, uav=uav, mission_name=mission_name, area=area_name)
        print('file created')
        return render(request, main)

我想更新文件的URL字段,以便在mission_name字段的末尾包含一个恒定的模式和格式。

object.url = f'127.0.0.1/twosecondgdt/{mission_name}'

如何实现?我应该在哪里存储此类代码,views.py或serializers.py?

回复