Search code examples
djangodjango-rest-frameworkmariadbdjango-serializerdatabase-versioning

Django (DRF) Serializers and MariDB Versioning Tables


MariaDB has this cool feature of tables versioning that allow you to keep track of data changes within a table like a VCS.

According to Django documentation, MariaDB is supported by the latest Django versions but it seems that this is not really the case since to query historical data you still have to use raw-sql commands.

Now I have nested serializers in my project and some of the models at the bottom of the "nesting" contain versioned objects. How do I specify what version (system-time) to fetch when requesting an endpoint view that uses the parent serializer of a nested serializer?

Example:

# MODELS

class Driver(models.Model):
    name = models.CharField(max_length=120)
    surname = models.CharField(max_length=120)
    car = models.ForeignKey(Car, on_delete=models.SET_NULL, null=True)

class Car(models.Model):
    brand = models.CharField(max_length=120)
    model = models.CharField(max_length=120)
    year = models.DateTimeField()
# SERIALIZERS

class CarSerializer(serializers.ModelSerializer):

    class Meta:
        model = Car
        fields = '__all__'

class DriverSerializer(serializers.ModelSerializer):
    car = CarSerializer()

    class Meta:
        model = Driver
        fields = '__all__'

Assuming the django model: Car has System Versioning with MariaDB (ALTER TABLE db.Car ADD SYSTEM VERSIONING;), how do I tell the Driver serializer to fetch a specific version of the Car data?


Solution

  • Thank you everyone for the comments, I may have solved this with the answer to this question: Override Django (DRF) Serializer object GET