模型生成到数据库中是否有顺序?
在我的项目中,我需要更改上传文件的位置。这是使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这并不能解决您的问题,因为
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.