I, пытаюсь найти самую чистую реализацию для доступа к контексту из подкласса meta в django serializer
views.py:
import someapp.models as models
import someapp.serializers as serializer
def some_view(request, batch_id):
exp_type = models.UploadModel.objects.filter(batch_id=batch_id).values()[0]['experiment_type']
model_idx = get_obj_idx_from_exp(model_names, exp_type)
use_model = model_objs[model_idx]
model_data = use_model.objects.filter(batch_id=batch_id)
list_data = serializer.GeneralSerializer(model_data, many=True, context={'model': use_model}).data
...
serializers.py
from rest_framework.serializers import ModelSerializer
from someapp import models
class GeneralSerializer(ModelSerializer):
# can access self.context['model'] here
class Meta:
# can NOT access self.context['model'] here
model = self.context['model']
fields, _ = zip(*self.context['model'].display_fields())