如何从模型中获取杂乱的图像文件名到视图django

时间:2021-10-19 16:05:08

I'm trying to get scrambled name of image that will be uploaded and send it to views. In views I know how to get real name of image but how do I get scrambled name from models?

我正在尝试将会被上传的图像的混乱名称发送给视图。在视图中,我知道如何获取图像的真实名称,但是如何从模型中获取混乱的名称呢?

I tried in this direction if someone knows I hope you can tell me do I go in right direction:

我试过这个方向如果有人知道我希望你能告诉我我走对了方向:

opts = UploadImage._meta
    print ([f.name for f in opts.fields])
    print ( [(field.name, getattr(UploadImage,field.name)) for field in UploadImage._meta.fields])
    print (UploadImage._meta.get_field('image'))
    print([f.name for f in opts.many_to_many])

models.py

models.py

from django.db import models
import uuid

def scramble_uploaded_filename(instance, filename):
    extension = filename.split(".")[-1]
    return "{}.{}".format(uuid.uuid4(), extension)

def filename(instance, filename):
    return filename

# Create your models here.
class UploadImage(models.Model):
#    print (scramble_uploaded_filename)
    image = models.ImageField("Uploaded image",upload_to=scramble_uploaded_filename)
    img_type = models.IntegerField("Image type")
    created = models.DateTimeField(auto_now_add=True)
    user = models.CharField(default='1',max_length= 100,editable=False)

views.py

views.py

import sys,os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from rest_framework import viewsets
from api.models import UploadImage
from api.serializers import UploadedImageSerializer
from rest_framework import authentication, permissions
from rest_framework.parsers import MultiPartParser,FormParser
import MySQLdb


class FileUploadViewSet(viewsets.ModelViewSet):
    #create queryset view
    permission_classes = (permissions.IsAuthenticated,)
    queryset = UploadImage.objects.filter(id=1,user='auth.User')
    serializer_class = UploadedImageSerializer
    parser_classes = (MultiPartParser, FormParser,)

    #after post action get this
    def perform_create(self, serializer):
        #grab request
        image_name = self.request.data['image']
        username = self.request.user
        #grab scrambled filename

1 个解决方案

#1


1  

image processing should be done in models if that is the reason you ask. Also inside fillen fuction you can find name of your image.

如果这是你要问的原因的话,图像处理应该在模型中完成。同样,在fillen功能中你可以找到你的图像的名字。

Add this to model.py

添加这个model.py

# Create your models here.

class UploadImage(models.Model):
#    print (scramble_uploaded_filename)
    image = models.ImageField("Uploaded image",upload_to=scramble_uploaded_filename)
    img_type = models.IntegerField("Image type")
    created = models.DateTimeField(auto_now_add=True)
    user = models.CharField(default='1',max_length= 100,editable=False)
    def filen(self):
        path = self.image.path
        print('image path',path)
        tip = self.captcha_type
        text = **here is your function that returns result of image process**
        return text 
    result = filen

#1


1  

image processing should be done in models if that is the reason you ask. Also inside fillen fuction you can find name of your image.

如果这是你要问的原因的话,图像处理应该在模型中完成。同样,在fillen功能中你可以找到你的图像的名字。

Add this to model.py

添加这个model.py

# Create your models here.

class UploadImage(models.Model):
#    print (scramble_uploaded_filename)
    image = models.ImageField("Uploaded image",upload_to=scramble_uploaded_filename)
    img_type = models.IntegerField("Image type")
    created = models.DateTimeField(auto_now_add=True)
    user = models.CharField(default='1',max_length= 100,editable=False)
    def filen(self):
        path = self.image.path
        print('image path',path)
        tip = self.captcha_type
        text = **here is your function that returns result of image process**
        return text 
    result = filen