def post(self, request, format=None): data = request.data serializer = ContentSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) ##serializer는 json api로 날라온걸 query set 으로 바꿔줄때도 쓰임 ##save: 함수안에 create가 이미 들어있어서 데이터베이스에 자동으로 저장됨 else: return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def get(self, request, format=None): contents = Content.objects.all() ##데이터베이스(엑셀 테이블)에서 데이터를 가져온 것. ## queryset이라는 형태로 저장이 되는데 이건 API 상태가 아니라서 serializer = ContentSerializer(contents, many=True) ##serializer 가 query set 을 API 형태로 만들어줌 return Response(serializer.data)
class CondoSerializer(ModelSerializer): slug = ReadOnlyField() condo_offer = BedroomQuantitySerializer(many=True) address = AddressSerializer() applications = ApplicationSerializer(many=True) condo_contents = ContentSerializer(many=True) class Meta: fields = '__all__' model = Condo lookup_field = 'slug' extra_kwargs = {'url': {'lookup_field': 'slug'}}
class UserSerializer(serializers.ModelSerializer): card = CardSerializer(many=True, read_only=True, source='card_sets') content = ContentSerializer(many=True, read_only=True, source='content_sets') class Meta: model = User fields = ('id', 'username', 'email', 'card', 'content', 'password') extra_kwargs = {'password': {'write_only': True}} def create(self, validated_data): user = User(username=validated_data['username']) user.set_password(validated_data['password']) user.save() return user
def get(self, request, pk, format=None): content = self.get_object(pk) serializer = ContentSerializer(content) return Response(serializer.data)
def get(self, request, format=None): results = Content.objects.filter(title__icontains=request.GET['text']) serializer = ContentSerializer(results, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
def post(self, request, format=None): user = User.objects.get(pk=request.data["user"]) contents = Content.objects.filter(Q(preference_one=user.preference_one) | Q(preference_two=user.preference_two) | Q(preference_one=user.preference_two) | Q(preference_two=user.preference_one)) serializer = ContentSerializer(contents, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
def post(self, request, format=None): contents = Content.objects.filter(genre=request.data["genre"]) serializer = ContentSerializer(contents, many=True) return Response(serializer.data, status=status.HTTP_200_OK)