I have a model BaseUser which inherits from AbstractUser and has two additional fields: complex_list and active_complex. complex_list is a ManyToManyField connected to the BaseComplex model through an enrollment table.
I want the default value for active_complex to be null which can be taken care of at the model initiation. I also need the user to be able to choose the active_complex only from the values within the complex_list. For that, I tried to use a customized ModelSerializer:
from rest_framework import serializers
from .models import BaseUser
class ActiveComplexSerializer(serializers.ModelSerializer):
this_user = serializers.SerializerMethodField('get_this_user')
choice =[]
qs = BaseUser.objects.get(username = this_user)
for cmplx in qs.complex_list:
choice.append((cmplx.id, cmplx.name))
active_complex = serializers.ChoiceField(choices = choice)
class Meta:
model = BaseUser
fields = ('active_complex')
def get_this_user(self, request):
return request.user
I know I'm doing something wrong because I get a query not found ERROR. I would appreciate it if you could correct this or suggest a better approach to achieving the same goal.