I am trying to create an upload file function in my website. However , i keep getting the error
The submitted data was not a file. Check the encoding type on the form. I tried to create a post function in views.py. Does this mean any files that i uploaded is not considered a file ?
models.py
class ClinicVisit(models.Model):
upload_attachment = models.FileField(null=True , blank=True, upload_to='uploads/')
views.py
class ClinicVisitList(generics.ListCreateAPIView):
serializer_class = ClinicVisitSerializer
filter_backends = [DjangoFilterBackend, filters.OrderingFilter]
filterset_class = ClinicVisitFilter
permission_classes = [permissions.IsAuthenticated,]
pagination_class = LargeResultsSetPagination
ordering = ['-created_at']
parser_class = (FileUploadParser,)
def get_queryset(self):
return ClinicVisit.objects.based_on_access_level(self.request.user)
def post(self, request, *args, **kwargs):
file_serializer = ClinicVisitSerializer(data=request.data)
print(request.data)
if file_serializer.is_valid():
file_serializer.save()
return response.Response(file_serializer.data, status=status.HTTP_200_OK)
else:
return response.Response(file_serializer.errors,status=status.HTTP_400_BAD_REQUEST)
serializers.py
class ClinicVisitSerializer(serializers.ModelSerializer):
upload_attachment = serializers.FileField()
class Meta:
model = ClinicVisit
exclude = clinicvisit_exclude
extra_kwargs = {
"staff_relationship": {"required": True},
"visit_type": {"required": True},
"visit_status": {"required": True},
}
Related
I am new in django and drf
in my project I have two group of permissions
1.normal_user group : with view_issue,view_project,view_analyzeissue
2.manager_user : with all permission as possible
i have some views that check some permissions
for example IssuesViewApi view, this view need to NormalUserPermissions
so i created new group with composition of permissions in my tests and send request to the view
my new group have view_issue,change_issue
when i send request to the IssuesViewApi i get 403 response
i have a NormalUserPermissions class
class NormalUserPermissions(permissions.BasePermission):
def has_permission(self, request: Request, view):
if request.user.has_perms(get_group_permissions("normal_user")):
return True
return False
class IssuesViewApi(generics.ListAPIView):
class IssueFilter(FilterSet):
labels = CharFilter(field_name="labels", lookup_expr='contains')
project_id = NumberFilter(field_name="project__id", lookup_expr='exact')
user_name = CharFilter(field_name="users__username", lookup_expr='exact')
start_date = DateFilter(field_name="updated_at", lookup_expr='gte')
end_date = DateFilter(field_name="updated_at", lookup_expr='lte')
class Meta:
model = Issue
fields = ["iid", 'is_analyzed', 'project_id', 'labels', 'user_name', 'start_date', 'end_date']
permission_classes = [IsAuthenticated, NormalUserPermissions]
http_method_names = ['get']
pagination_class = StandardPagination
queryset = Issue.objects.all()
serializer_class = IssueSerialize
filter_backends = [OrderingFilter, DjangoFilterBackend]
filterset_class = IssueFilter
ordering_fields = ['iid', 'weight'] # order fields depend on user request
ordering = ['iid'] # default order value
def get(self, request, *args, **kwargs):
response = super(IssuesViewApi, self).get(request, *args, **kwargs)
return Response({
'data': {
'issues': response.data['results'],
},
'paginationInfo': {
"count": response.data['count'],
"next_page": response.data['next'],
"previous_page": response.data['previous'],
"total_pages": self.paginator.page.paginator.num_pages
}
})
def test_create_custom_group_and_filter_issues_and_update_issue(self):
self.run_fake_discovery()
user = self.get_user()
user.groups.add(Group.objects.get_by_natural_key("manager_user"))
self.login(username=user.username, password="123456789")
add_group_url = reverse('group-add')
group_name = "new_group"
group_permissions = list(Permission.objects.filter(codename__in=['view_issue', 'change_issue']).all().values_list('id', flat=True))
response = self.client.post(add_group_url, data=json.dumps({'name': group_name, 'permissions': group_permissions}), content_type=self.CONTENT_TYPE)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertTrue(Group.objects.filter(name=group_name).exists())
sync_user_groups_url = reverse('sync-users-and-groups')
test_user = User.objects.get(username='testuser')
response = self.client.post(sync_user_groups_url, data=json.dumps({'group_name': group_name, 'users': [test_user.id]}), content_type=self.CONTENT_TYPE)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertTrue(test_user.groups.filter(name=group_name).exists())
response=self.logout()
self.assertEqual(response.status_code,status.HTTP_200_OK)
self.login(username=test_user.username, password='123456789')
filter_issue_url = reverse('issue-filter')
filter_issue_response = self.client.get(filter_issue_url, data={'username': user.username}, content_type=self.CONTENT_TYPE)
self.assertEqual(filter_issue_response.status_code, status.HTTP_200_OK)
Why all permissions checks ?
Im going to when user has view_issue permission then get response with 200 status
Views
class AuthDataViewSet(ModelViewSet):
queryset = AuthData.objects.all()
serializer_class = AuthDataSerializer
def create(self, request, *args, **kwargs):
serializer_data, headers = create_auth_data(self, request.data, {'request': request})
# returning response with the data
create_auth_data function
def create_response_data(view, data: dict = None, context: dict = None):
# I calling the viewset methods below
serializer = view.get_serializer(data=data, context=context)
serializer.is_valid(raise_exception=True)
view.perform_create(serializer)
headers = view.get_success_headers(serializer.data)
return serializer.data, headers
Serializer
class AuthDataSerializer(ModelSerializer):
class Meta:
model = AuthData
fields = ('login', 'password', 'project', 'manager')
def create(self, validated_data):
validated_data['manager'] = self.context['request'].user.manager
return validated_data
I got the correct serializer.data, no errors and pure data, but the instance didn't saved to the database.
Give this a try:
class AuthDataSerializer(ModelSerializer):
class Meta:
model = AuthData
fields = ('login', 'password', 'project', 'manager')
def create(self, validated_data):
auth_data = AuthData.objects.create(**validated_data)
return auth_data
I am trying to upload both text data and a file to django server using angular for the front-end,
here is my models in django
#models.py
class UploadChapter(models.Model):
chapter_number = models.CharField(max_length=50, choices=chapters)
chapter_file = models.FileField(upload_to="files")
student = models.ForeignKey(User,on_delete=models.CASCADE)
supervisor = models.ForeignKey(Supervisor, on_delete=models.CASCADE)
status = models.CharField(max_length=100, choices=status, default="Pending")
def __str__(self):
return f'{self.student} - {self.chapter_number}'
for the serializer I wrote this code
#serializers.py
class UploadChapterSerializer(serializers.ModelSerializer):
class Meta:
model = UploadChapter
fields = ['chapter_number', 'chapter_file', 'student', 'supervisor', 'status']
and for the views i wrote this code
#views.py
#csrf_exempt
def ChapterApi(request, id=0):
if request.method=='GET':
chapters=UploadChapter.objects.all()
chaptersSerializer=UploadChapterSerializer(chapters, many=True)
return JsonResponse(chaptersSerializer.data, safe=False)
elif request.method=='POST':
chaptersData=JSONParser().parse(request)
chaptersSerializer=UploadChapterSerializer(data=chaptersData)
if chaptersSerializer.is_valid():
chaptersSerializer.save()
return JsonResponse('Added Successfully', safe=False)
return JsonResponse('Failed', safe=False)
so am getting errors from the JSONParser and I really dont know how to do this, I need help please
I found an answer for myself, i wrote a class-based view instead of the function based, here is the code for it
class ChapterView(APIView):
parser_classes = (MultiPartParser, FormParser)
def post(self, request, *args, **kwargs):
chapter_serializer = UploadChapterSerializer(data=request.data)
if chapter_serializer.is_valid():
chapter_serializer.save()
return JsonResponse('Chapter Sent Successfully', safe=False)
else:
return JsonResponse('upload fail', safe=False)
def get(self, request):
chapter = UploadChapter.objects.all()
chapterSerializer = UploadChapterSerializer(chapter, many=True)
return JsonResponse(chapterSerializer.data, safe=False)
I'm getting an error where I tried to patch up but I don't understand where should I change the code.
This is my serializer.py file:
class TeacherSignupSerializer(serializers.ModelSerializer):
user = UserCreateSerializer()
# teacher = SchoolSerializer(read_only=True)
# teachers = ClassSerializer(read_only=True)
# teach = SubjectSerializer(read_only=True)
class Meta:
model = Teacher
fields = (
'name',
'email',
'mobile_number',
'city',
'school_name',
'subject',
'user',
'associated_class',
)
extra_kwargs = {'password': {'write_only': True}}
def create(self, validated_data):
user_data = validated_data.pop('user')
user = User.objects.create_user(**user_data)
teacher = Teacher.objects.create(**validated_data)
teacher.set_user(user)
return teacher
And this is my views.py:
class TeacherSigupAPIView(generics.GenericAPIView):
serializer_class = TeacherSignupSerializer
def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
if serializer.is_valid():
teacher = serializer.save()
if teacher:
return Response({
"status": True,
"teacher": TeacherSerializer(teacher, context=self.get_serializer_context()).data,
})
return Response({
"status": False,
"message": "Teacher is already exists"
}, status=status.HTTP_400_BAD_REQUEST)
The error I got was (place_error_here) but when I changed my code, that also gave the same issue.
Edit
I suspect the whole problem with my UpdateApiView is with the url. No matter how I change it, will return 404.
url(r'verify-phone/(?P<phone_id>^\d+)$', view.VerifyPhone.as_view(), name='verify-phone'),
it returns
{
"detail": "Not found."
}
[18/Apr/2016 01:39:02] "PATCH /api/verify-phone/phone_id=00980 HTTP/1.1" 404 4941
Why?
views.py
class VerifyPhone(generics.UpdateAPIView):
permission_classes = (AllowAny,)
serializer_class = serializers.VerifyPhoneSerializer
allowed_methods = ['PATCH']
lookup_field = 'phone_id'
def get_queryset(self):
phone_id = self.request.query_params.get('phone_id', None)
queryset = User.objects.filter(phone_id=phone_id)
return queryset
def update(self, request, *args, **kwargs):
print('inside update')
print(request.data)
partial = kwargs.pop('partial', False)
instance = self.get_object()
print(instance)
serializer = self.get_serializer(instance, data=request.data, partial=partial)
print(serializer)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)
print('done perform update')
return Response(serializer.data)
serializers.py
class VerifyPhoneSerializer(serializers.ModelSerializer):
regex = r'\d+'
verification_code = serializers.RegexField(regex, max_length=7, min_length=7, allow_blank=False)
phone_id = serializers.HyperlinkedRelatedField(view_name='verify-phone', lookup_field='phone_id', read_only=True)
class Meta:
model = User
fields = ('verification_code', 'phone_id')
def validate(self, data):
verification = api.tokens.verify(data['phone_id'], data['verification_code'])
if verification.response.status_code != 200:
raise serializers.ValidationError("Invalid verification code.")
return data
def update(self, instance, validated_data):
instance.phone_number_validated = True
instance.save()
return instance
Second question Is this correct to get phone_id from the views?
phone_id = serializers.HyperlinkedRelatedField(view_name='verify-phone', lookup_field='phone_id', read_only=True)
Looking at your api url def, I think you should call:
/api/verify-phone/00980
instead of
/api/verify-phone/phone_id=00980
I also think something is wrong with the url def itself (the ^ before \d):
url(r'verify-phone/(?P<phone_id>^\d+)$', view.VerifyPhone.as_view(), name='verify-phone')
should be
url(r'verify-phone/(?P<phone_id>\d+)$', view.VerifyPhone.as_view(), name='verify-phone')
or
url(r'verify-phone/(?P<phone_id>\d{5})$', view.VerifyPhone.as_view(), name='verify-phone')