pythondjangodjango-modelsdjango-rest-frameworkdjango-serializer

Is there a better way to update all relation rows with `ForegnKey` relation in `Django ORM`


I have two models, one like this:

class PhysicalSensor(models.Model):
    name = models.CharField(unique=True, max_length=255)

    def __str__(self) -> str:
        return self.name

class Sensor(models.Model):
    physical_sensor = models.ForeignKey(PhysicalSensor, on_delete=models.RESTRICT, null=True, blank=True)

So, when I want to add a PhisycalSensor record, I want to set the Sensor records to that. Right now I handle this in my Serializer class:


class PhysicalSensorSerializer(ModelSerializer):
    sensors = SensorSerialiazer(required=False, many=True)

    class Meta:
        fields = ("__all__")
        model = PhysicalSensor

    def create(self, validated_data):
        sensors = validated_data.pop("sensors")
        ph_sensor = super().create(validated_data)

        for sensor in sensors:
            s = Sensor.objects.get(sensor["id"])
            s.physical_sensor = ph_sensor
            s.save()

and for edit of PhysicalSensor, I use the same thing in .update() method of my Serializer.

Is there any batter way of doing this or a best practice for this ?


Solution

  • The best way is to use queryset.update method:

     def create(self, validated_data):
        sensors = validated_data.pop("sensors")
        ph_sensor = super().create(validated_data)
    
        ids = (sensor["id"] for sensor in sensors)
        Sensor.objects.filter(pk__in = ids).update(physical_sensor=ph_sensor)
    

    more here: https://docs.djangoproject.com/en/5.0/ref/models/querysets/#update