DRF: Изменение значения на основе связанной модели

Я хочу установить значение в поле, которое зависит от другого поля из другой модели. У меня есть модели CollectionObject и Transaction, между которыми существует связь "многие-ко-многим". Мой код выглядит следующим образом:

collectionobject/model.py:

from django.db import models
from collectingevent.models import CollectingEvent

# Create your models here.
## Collection Object
class CollectionObject(models.Model):
    ID = models.IntegerField(primary_key=True, editable=False) 
    inventory_number = models.CharField(max_length=500)
    remarks = models.CharField(max_length=500)
    collecting_event = models.ForeignKey(CollectingEvent, related_name='collection_object', on_delete=models.DO_NOTHING)    # many-to-1

    class Meta:
        #ordering = ['pk']
        db_table = 'collection_object'

transaction/models.py:

from django.db import models
from collectionobject.models import CollectionObject


## Transaction
class Transaction(models.Model):

    class TransactionType(models.TextChoices):
        LOAN = 'loan',
        DIGITIZATION = 'digitization',
        MOVEMENT = 'movement', 
        RESAMPLING = 'resampling'

    ID = models.IntegerField(primary_key=True, unique=True, editable=False)
    collection_object_ID = models.ManyToManyField(CollectionObject, related_name='status')#, on_delete=models.DO_NOTHING)#CollectionObject, related_name='transaction')#, on_delete=models.DO_NOTHING) # many-to-many
    transaction_type = models.CharField(max_length=500) # TODO: here choices
    transaction_date = models.DateTimeField()
    remarks = models.CharField(max_length=500)

    class Meta:
        db_table = 'transaction'

collectionobject/serializer.py:

from rest_framework import serializers
from .models import *
from collectingevent.models import CollectingEvent
from transaction.serializers import TransactionSerializer

class CollectionObjectSerializer(serializers.ModelSerializer):

    # many-to-1
    collecting_event = serializers.PrimaryKeyRelatedField(queryset=CollectingEvent.objects.all(),
                                                        many=False)
    
    # 1-to-many
    determination = serializers.PrimaryKeyRelatedField(many=True, read_only=True)

    # transaction
    available = serializers.BooleanField(default=True)
    status = TransactionSerializer(many=True)

    class Meta:
        model = CollectionObject
        #fields = "__all__"
        fields = (
                'ID',
                'inventory_number',
                'available',
                'status',
                'remarks',
                'collecting_event', 
                'determination',
                )

И transaction/serializer.py:

from rest_framework import serializers
from .models import *
        
    
class TransactionSerializer(serializers.ModelSerializer):

    class Meta:
        model = Transaction
        fields = (
            'ID',
            'collection_object_ID',
            'transaction_type',
            'transaction_date',
            'remarks')

Я следовал некоторым другим вопросам типа Django / DRF - Get field of related model, но как только я отправляю запрос с

get_response = requests.post(endpoint, {
                            "collection_object_ID": 1,
                            "transaction_type": "loan",
                            "transaction_date": "2022-05-06 00:00:00",
                            "remarks": "loan issued"
    },)

Я получаю в status все поля из transaction, как вы можете видеть здесь enter image description here.

Что я хочу достичь, так это просто установить размещенное transaction_type в status и соответственно изменить available. В принципе, это должно выглядеть следующим образом:

enter image description here

Вернуться на верх