def post(self, request, format=None): serializer = AccountsSerializer(data=request.data, context={'request': request}) if (serializer.is_valid()): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, pk, format=None): user = self.get_user(pk) serializer = AccountsSerializer(user, data=request.data, context={'request': request}) # level 2 if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class PostSerializer(serializers.ModelSerializer): user = AccountsSerializer(read_only=True) user_id = serializers.PrimaryKeyRelatedField( queryset=Accounts.objects.all(), write_only=True) tasks = TaskSerializer(many=True) comments = CommentSerializer(many=True, read_only=True) favorites = FavoriteSerializer(many=True, read_only=True) class Meta: model = Post fields = [ 'id', 'title', 'detail', 'limit', 'user', 'user_id', 'tasks', 'comments', 'favorites' ] def create(self, validated_data): tasks_data = validated_data.pop('tasks') validated_data['user'] = validated_data.get('user_id', None) if validated_data['user'] is None: raise serializers.ValidationError("user not found.") del validated_data['user_id'] post = Post.objects.create(**validated_data) for task_data in tasks_data: Task.objects.create(post=post, **task_data) return post
class UserProfileSerializer(serializers.ModelSerializer): accounts_linked = AccountsSerializer(many=True) cards_linked = CardSerializer(many=True) transactions_linked = TransactionsSerializer(many=True) tickets_linked = TicketSerializer(many=True) wallets_linked = WalletSerializer(many=True) class Meta: model = UserProfile fields = ('first_name', 'last_name', 'email_address', 'phone_number', 'alternate_phone', 'kyc_verified', 'profile_pic', 'user_joined_date', 'accounts_linked', 'tickets_linked', 'transactions_linked', 'wallets_linked', 'cards_linked')
class CommentSerializer(serializers.ModelSerializer): user = AccountsSerializer(read_only=True) user_id = serializers.PrimaryKeyRelatedField( queryset=Accounts.objects.all(), write_only=True) post_id = serializers.PrimaryKeyRelatedField(queryset=Post.objects.all(), write_only=True) class Meta: model = Comment fields = ['id', 'content', 'user', 'user_id', 'post_id'] def create(self, validated_data): validated_data['user'] = validated_data.get('user_id', None) if validated_data['user'] is None: raise serializers.ValidationError("user not found.") del validated_data['user_id'] validated_data['post'] = validated_data.get('post_id', None) if validated_data['post'] is None: raise serializers.ValidationError("post not found.") del validated_data['post_id'] comment = Comment.objects.create(**validated_data) return comment
class TaskSerializer(EnumSupportSerializerMixin, serializers.HyperlinkedModelSerializer): # new one # AFTER USING META # owner = serializers.ReadOnlyField(source='owner.username') owner = AccountsSerializer(read_only=True) class Meta: model = Task fields = [ 'id', 'url', 'owner', 'created', 'title', 'details', 'status', 'due_date' ] def create(self, validated_data): return Task.objects.create(**validated_data) def update(self, instance, validated_data): instance.title = validated_data.get('title', instance.title) instance.details = validated_data.get('details', instance.details) instance.owner = validated_data.get('owner', instance.owner) instance.save() return instance
def get(self, request, activation_key=None): account = Account.objects.get(activation_key=activation_key) if account and account.has_usable_password(): return HttpResponse("account_already_active/invalid_key") serializer = AccountsSerializer(account) return Response(serializer.data)
def get(self, request): serializer = AccountsSerializer(request.user) return Response(serializer.data)
class StoreSerializer(serializers.ModelSerializer): account = AccountsSerializer(required=False) class Meta: model = Store fields = '__all__'
class FavoriteSerializer(serializers.ModelSerializer): user = AccountsSerializer(read_only=True) class Meta: model = Favorite fields = ['user']
def get(self, request, pk, format=None): user = self.get_user(pk) serializer = AccountsSerializer(user, context={'request': request}) return Response(serializer.data)
def get(self, request, format=None): users = User.objects.all() serializer = AccountsSerializer(users, many=True, context={'request': request}) return Response(serializer.data)