Answer the question
In order to leave comments, you need to log in
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
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question