如何将文件自动上传到后端的文件中上传,而无需在前端进行手动选择

发布于 2025-02-07 05:28:21 字数 1310 浏览 0 评论 0原文

我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿看小鸭子会跑 2025-02-14 05:28:21

您可以在文件字段中使用默认值并提供文件的路径。如果您已经在设置中设置了所有媒体和静态文件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')

这将使您的对象链接到该文件,以为您具有“媒体/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.

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')

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文