如何将文件自动上传到后端的文件中上传,而无需在前端进行手动选择
我的models.py
from django.db import models
# Create your models here.
class Result(models.Model):
Id = models.AutoField(primary_key=True, blank=False)
Name = models.CharField(max_length=100)
# Date = models.DateTimeField(auto_now=False, auto_now_add=False)
# Comments = models.TextField(max_length=256)
File = models.FileField(blank=False)
我的views.py
from django.shortcuts import render
from contextmapping.Connection import Connection
from rest_framework.response import Response
from rest_framework.decorators import action
from django.shortcuts import render
from rest_framework import viewsets,status
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated
from result.models import Result
class ResultViewSet(viewsets.ModelViewSet):
queryset = Result.objects.all()
serializer_class = Result
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)
@action(detail=True,methods=['GET'])
def resultfill(self,request,pk=None):
response={'message':'its working'}
return Response(response,status=status.HTTP_200_OK)
我在文件夹中有一个名为data1.py的文件,我想在url中运行resultfill函数。在型号中文件。如何实现?
My models.py
from django.db import models
# Create your models here.
class Result(models.Model):
Id = models.AutoField(primary_key=True, blank=False)
Name = models.CharField(max_length=100)
# Date = models.DateTimeField(auto_now=False, auto_now_add=False)
# Comments = models.TextField(max_length=256)
File = models.FileField(blank=False)
My views.py
from django.shortcuts import render
from contextmapping.Connection import Connection
from rest_framework.response import Response
from rest_framework.decorators import action
from django.shortcuts import render
from rest_framework import viewsets,status
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated
from result.models import Result
class ResultViewSet(viewsets.ModelViewSet):
queryset = Result.objects.all()
serializer_class = Result
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)
@action(detail=True,methods=['GET'])
def resultfill(self,request,pk=None):
response={'message':'its working'}
return Response(response,status=status.HTTP_200_OK)
I have a file named data1.py in a folder, I want to run resultfill function in views.py by url, and want to give a path to this file and that file should be automatically upload to File in models.py . How to achieve it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在文件字段中使用默认值并提供文件的路径。如果您已经在设置中设置了所有媒体和静态文件URL,则可以将“ data1.py”添加为默认值。
这将使您的对象链接到该文件,以为您具有“媒体/data1.py”为文件位置,以防万一您不从Frontend或后端上传自己的文件。
You can use default in your file field and give the path of the file. If you have set up all the media and static file URLS in settings then you can just add 'data1.py' as default.
This will link your objects to that file imagining that you have 'media/data1.py' as your file location in case you don't upload your own file from frontend or backend.