【问题标题】:Django models.FileField(upload_to=function1, default=function2)Django 模型.FileField(upload_to=function1, default=function2)
【发布时间】:2015-11-09 20:07:05
【问题描述】:

我正在尝试使用函数在 django 模型中设置默认图像。原因是我生成了图像(一个二维码),我想用与 request.user 关联的模型保存它,它正在使用 upload_to= 但默认失败,它似乎没有得到实例.

代码是: --models.py --

from django.db import models
from django.contrib.auth.models import User
import django.utils.timezone as timezone
from myproject.settings import MEDIA_ROOT
import os

def function1(instance, filename):
    retun os.path.join(self.uid.username, str(instance.shortname), '.jpg')

def function2(instance):
    return os.path.join(MEDIA_ROOT, str(instance.username), 'tmp.jpg')

class MyModel(models.Model):
    uid = models.ForeignKey(User)
    shortname = models.CharField()
    qr = models.FileField(upload_to=function1, default=function2)

这使得 function2() 只需要 1 个参数(给定 0)

我也尝试像这样将 uid 作为参数传递给 function2:

def function2(uid):
    username = uid.username
    return os.path.join(MEDIA_ROOT, username, 'tmp.jpg')

class MyModel(models.Model):
    uid = models.ForeignKey(User)
    shortname = models.CharField()
    qr = models.FileField(upload_to=function1, default=function2(uid))

但这也不起作用,给出一个 AttributeError 异常: “ForeignKey”对象没有“用户名”属性

关于如何告诉 django 上传本地生成的文件的任何想法(在 django 机器上的 MEDIA_ROOT/request.user.username/ 上生成)

谢谢!!

【问题讨论】:

  • 为什么你会认为默认函数会被传递给实例?事实并非如此,而且情况一直如此。

标签: django django-models


【解决方案1】:

我从这里的视图中找到了解决问题的方法: Loading Django FileField and ImageFields from the file system

【讨论】:

  • 感谢您的回答!会尝试任何方式!
  • 这个答案传达了您需要首先为 FileField 创建一个 django.core.files.File 对象的想法。
【解决方案2】:

您需要使用实例而不是自我。此外,function2 不会被传递给实例。

【讨论】:

    【解决方案3】:

    您需要使用instance 而不是selfself 不存在于function1function2 范围内:

    def function1(instance, filename):
        return os.path.join(instance.uid.username, str(instance.shortname), '.jpg')
    
    def function2(instance):
        return os.path.join(MEDIA_ROOT, str(instance.username), 'tmp.jpg')
    
    class MyModel(models.Model):
        uid = models.ForeignKey(User)
        shortname = models.CharField()
        qr = models.FileField(upload_to=function1, default=function2)
    

    您可以找到FileField.upload_to here 的文档

    你得到function2() takes exactly 1 argument, (0 given) 因为function2 期待instance 参数,如果你尝试像这样传递uid

    class MyModel(models.Model):
        uid = models.ForeignKey(User)
        shortname = models.CharField()
        qr = models.FileField(upload_to=function1, default=function2(uid))
    

    将引发错误,因为此时 uidmodels.ForeignKey 实例。您可以尝试在pre_save 信号中执行此操作:

    def function2(sender, instance, *args, **kwargs):
    
        if not instance.qr:
            instance.qr = os.path.join(MEDIA_ROOT, username, 'tmp.jpg')
    
    pre_save.connect(function2, sender=MyModel)
    

    【讨论】:

    • 实例与自身在复制代码时出现错误...我在第一篇文章中对其进行了编辑以更正它谢谢!,试过了,但它仍然说:function2() 只需要一个参数, (给定 0 个)
    猜你喜欢
    • 2020-06-29
    • 2018-11-08
    • 2013-07-06
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    相关资源
    最近更新 更多