S
S
Sergey Alekseev2018-03-16 07:54:48
React
Sergey Alekseev, 2018-03-16 07:54:48

Django rest framework, how to properly update a list of something?

Good morning, sending data from the front

export function addProject(typeProject) {

    axios.post("http://127.0.0.1:8000/add_project/", {
        userId: window.localStorage.getItem("userId"),
        typeProject: typeProject
    })
        .then(res => {
            console.log(res);
        })
        .catch(error => {
            console.log(error)
        });
}


route
router.register(r'add_project', AddProjectView)


views
class AddProjectView(mixins.CreateModelMixin, mixins.RetrieveModelMixin,
                     mixins.UpdateModelMixin, viewsets.GenericViewSet):
    queryset = Project.objects.all()
    serializer_class = ProjectSerializer
    authentication_classes = (TokenAuthentication,)

    def put(self, request):
        """Create project."""
        instance = self.queryset
        return self.update(request, instance=instance)

serializer
class ProjectSerializer(serializers.ModelSerializer):

    class Meta:
        model = Project
        fields = ('name', 'projecttype')

    def update(self, instance, validated_data):
        """
        Create the object.
        :param instance:
        :param validated_data: string
        """
        user_id = validated_data["userId"]
        user = User.objects.get(id=user_id)
        print(user)
        user_account = UserAccount.objects.get(user=user)
        customer = Customer.objects.get(useraccount=user_account)
        instance.customer = customer
        instance.name = "PROJECT 001"
        instance.projecttype = validated_data["typeProject"]
        instance.save()

        return instance

Everything seems to be clear, but it does not work. Returns an error 400. Tell me where the error is

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
marataziat, 2018-03-16
@marataziat

Check ALLOWED_HOSTS = ['*'] in settings.py and

CORS_ORIGIN_WHITELIST = (
 ‘localhost:8080’,
)
https://medium.com/@peterretief/django-rest-framew...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question