[英]How to upload a file in File Filed of Django automatically in backend without manual selection in front-end
我的模型.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)
我的观点.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 在 views.py 中运行 resultfill 函数,并希望提供该文件的路径,并且该文件应自动上传到 models.py 中的文件。 如何实现呢?
您可以在文件字段中使用默认值并给出文件的路径。 如果您在设置中设置了所有媒体和静态文件 URL,那么您可以添加“data1.py”作为默认值。
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(default='data1.py')
这会将您的对象链接到该文件,假设您将“media/data1.py”作为您的文件位置,以防您不从前端或后端上传自己的文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.