模型生成到数据库中是否有顺序?

发布于 2024-10-05 03:07:55 字数 1075 浏览 0 评论 0原文

在我的项目中,我需要更改上传文件的位置。这是使用 FileSystemStorage 完成的。文件上传的路径应该易于配置,例如使用 Django Admin。

from django.core.files.storage import FileSystemStorage  
from django.db import models  

class Setting(models.Model):  
    entry = models.CharField(primary_key=True, db_column="entry", max_length=50)  
    value = models.CharField(db_column="value", max_length=250, blank=True, null=True)

    def __unicode__(self):
        return "%s" %(self.entry)

    class Meta:
        db_table = 'settings'
        verbose_name = 'Setting'
        verbose_name_plural = 'Settings'    

fs =  FileSystemStorage(location=Setting.objects.get(entry__exact='upload_path').value)      

def generate_filename(instance, filename):  
    ...

class FileImport(models.Model):
    data_file = models.FileField(_('Data file'), upload_to=generate_filename, storage=fs)

我收到此错误:

django.db.utils.DatabaseError: 关系“设置”不存在 从“settings”中选择“settings”.“entry”、“settings”.“value”。

第 1 行:...对于正在创建 FileSystemStorage 的行, 有没有办法告诉 Django 首先创建表设置(用于设置对象),然后用一些固定装置填充该表?

In my project I need to change the location where files are being uploaded. This is done using a FileSystemStorage. The path were the files are uploaded should be easy to configure, for example using the Django Admin.

from django.core.files.storage import FileSystemStorage  
from django.db import models  

class Setting(models.Model):  
    entry = models.CharField(primary_key=True, db_column="entry", max_length=50)  
    value = models.CharField(db_column="value", max_length=250, blank=True, null=True)

    def __unicode__(self):
        return "%s" %(self.entry)

    class Meta:
        db_table = 'settings'
        verbose_name = 'Setting'
        verbose_name_plural = 'Settings'    

fs =  FileSystemStorage(location=Setting.objects.get(entry__exact='upload_path').value)      

def generate_filename(instance, filename):  
    ...

class FileImport(models.Model):
    data_file = models.FileField(_('Data file'), upload_to=generate_filename, storage=fs)

I receive this error:

django.db.utils.DatabaseError:
relation "settings" does not exist
LINE 1: ...ELECT "settings"."entry", "settings"."value" FROM "settings"...

for the line where FileSystemStorage is being created. Is there a way of telling Django to create table settings(for Setting objects) first and then fill this table with some fixtures?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

水染的天色ゝ 2024-10-12 03:07:55

这并不能解决您的问题,因为 Settings 表仍然不会被填充。将其移至类属性中,该属性在第一次实例化时进行初始化。

That won't solve your problem, since the Settings table still won't be populated. Move it into a class attribute that gets initialized the first time it's instantiated.

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