serializers.pyにフィルタコードを書くにはどうしたらいいですか? 私は以下のモデルを持っています。Djangoで複数のフィルタを書くには
User (AbstractBaseUser)
userid = models.CharField(max_length=64, primary_key=True)
username = models.CharField(max_length=128)
Clinic (models.Model)
clinic_id = models.CharField(max_length=8, primary_key=True)
clinic_name = models.CharField(max_length=64)
Consultation (models.Model)
consultation_id = models.AutoField(primary_key=True)
clinic_id = models.ForeignKey(Clinic)
user = models.ForeignKey(User)
Chat (models.Model)
chat_id = models.AutoField(primary_key=True)
consultation_id = models.ForeignKey(Consultation)
DIDRESPONSE_CHOICE = {
('R', 'No'),
('C', 'Doing'),
('F', 'Done'),
}
did_response = models.CharField(max_length=1, choices=DIDRESPONSE_CHOICE, default='N')
ChatDetail (models.Model)
chat_no = models.AutoField(primary_key=True)
chat_id = models.ForeignKey(Chat)
CHATFROM_CHOICE = {
('D', 'Doctor'),
('U', 'User'),
}
chat_from = models.CharField(max_length=1, blank=True, null=True)
chat = models.CharField(max_length=512, blank=True)
上記のデータとクライアントへの応答をJSON形式でシリアル化する必要があります。
{
'username': 'Tom Smith',
'clinic_name': 'Adam's clinic',
'consultation_id': 12345678,
'chat_id': 09876543,
'chat_detail' : [
{'chat_no': 11122233, 'chat_from': 'D', 'chat': 'How are you doing today?'},
{'chat_no': 11112234, 'chat_from': 'U', 'chat': 'I have a headache'},
{'chat_no': 11122235, 'chat_from': 'D', 'chat': 'Oh, I'm sorry'},
{'chat_no': 11122236, 'chat_from': 'U', 'chat': 'Can you help me?'},
]
}
override_get_querysetメソッドを使用してviews.pyでフィルタを作成しました。しかし、それは仕事ではありませんでした。だから、私はserializers.pyにフィルタを書き込もうとしました。しかし、それも動作しませんでした(私は私のserializers.pyの上の要件のフィルタを書くことができません)。
私の要件を達成するにはどうすればよいですか?私を助けてください。
views.py
class WebViewChatDataOutputView(APIView):
def get(self, request, format=None):
chat_detail = ChatDetail
serializer = WebViewChatDataOutputSerializer(chat_detail, many=True)
filter_fields = ('chat_id')
return Response(serializer.data)
def get_queryset(self):
consultation_id = self.request.consultation_id
chat = Chat.objects.filter(consultation_id=consultation_id)
return ChatDetail.objects.filter(chat_id=chat.chat_id)
serializers.py
class WebViewChatDataOutputSerializer(serializers.ModelSerializer):
# class Meta:
# model = Consultation
# fields = ('consultation_id')
#
# class Meta:
# model = Chat
# fields = ('chat_id')
#
class Meta:
chat = ChatDataForChatDetailSerializer(many=True)
model = ChatDetail
fields = ('chat_no', 'chat_from', 'chat')
私は、フィルタを作成しようとしています。しかし、私は 'Meta.fields'を手に入れました。 ChatDetail、「エラーチャット
私の見解は
from .filtering import CategoryFilter
class WebViewChatDataOutputView(generics.RetrieveAPIView):
def get(self, request, format=None):
filter_backends = (
rest_framework.filters.OrderingFilter,
rest_framework.filters.DjangoFilterBackend
)
filter_class = CategoryFilter
serializer = WebViewChatDataInputSerializer(chat_detail, many=True)
return Response(serializer_class.data)
以下であると私filtering.pyは次の通りである
class CategoryFilter(rest_framework.filters.FilterSet):
name = django_filters.CharFilter(
name=ChatDetail,
lookup_expr='chat_id'
)
parent_category_name = django_filters.CharFilter(
name=Chat,
lookup_expr='chat_id'
)
class Meta:
model = ChatDetail
fields = ['ChatDetail', 'Chat']
ました:このフィルタセットに定義されていないフィールドが含まれています。。。私は
。アドバイスをGIMEしてください?文法のいずれかの種類を間違え私は自分のコードを更新しました。
views.py
class WebViewChatDataOutputView(generics.RetrieveAPIView):
def get(self, request, format=None):
lookup_field = 'chat_id'
queryset = Chat.objects.all()
serializer_class = ChatSerializer
return Response(serializer_class.data)
serializers.py
class ChatDetailSerializer(serializers.ModelSerializer):
class Meta:
model = ChatDetail
fields = '__all__'
class ChatSerializer(serializers.ModelSerializer):
username = serializers.CharField(source='consultation_id.user.username') # You should rename the consultation_id foreign key field to consultation, by the way
clinic_name = serializers.CharField(source='consultation_id.clinic_id.clinic_name') # Same goes for clinic_id
chat_detail = ChatDetailSerializer(many=True, source='chatdetail_set')
class Meta:
model = Chat
fields = ('user_name', 'clinic_name', 'consultation_id', 'chat_id', 'chat_detail')
あなたのビューコードを表示できますか? – Thaian
ありがとうございます。 views.pyとserializers.pyを追加しました。私を助けてくれますか? –
あなたの問題を解決しましたか? – Thaian