uploadfile_ptr来自何时使用inherit创建模型?

时间:2018-01-11 04:41:28

标签: python django

我将上传文件模型test.iloc[test.index.get_loc('2018-01-08') - 1] 0 5 Name: 2018-01-05, dtype: int64 更改为继承自基本模型,如bellow:

基础模型:

WorkOrderUploadFile

我的def generate_files_directory(instance, filename): url = "%s/%s" % (instance.filepath , filename) # will output something like `images/imgs/test/` return url class UploadFile(models.Model): """ Base upload model """ #filepath = models.CharField(max_length=128, default="images/qiyun_admin_servicemanage_workorder/") file = models.FileField(upload_to=generate_files_directory) ctime = models.DateTimeField(auto_now_add=True) uptime = models.DateTimeField(auto_now=True) def __str__(self): return self.file def __unicode__(self): return self.file 课程:

WorkOrderUploadFile

当我进行游戏时,我得到了以下问题:

class WorkOrderUploadFile(UploadFile):
    """
    work order upload file
    """
    filepath = models.CharField(max_length=128, default="images/qiyun_admin_servicemanage_workorder/")

但我没有在我的基础模型中添加字段名$ python3 manage.py makemigrations You are trying to add a non-nullable field 'uploadfile_ptr' to workorderuploadfile without a default; we can't do that (the database needs something to populate existing rows). Please select a fix: 1) Provide a one-off default now (will be set on all existing rows with a null value for this column) 2) Quit, and let me add a default in models.py 以及如何处理它?我尝试将uploadfile_ptr提供给值但我仍然收到错误,它会报告错误:

1

修改

基础模型和子类模型不在同一个项目中。

修改-2

我在我的基本模型中添加了默认值。

File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/MySQLdb/cursors.py", line 374, in _do_query
    db.query(q)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/MySQLdb/connections.py", line 277, in query
    _mysql.connection.query(self, query)
django.db.utils.IntegrityError: (1062, "Duplicate entry '1' for key 'PRIMARY'")

但仍有这个问题。

修改-3

我尝试将Meta添加到基本模型仍然不起作用:

file = models.FileField(upload_to=generate_files_directory, default="")

1 个答案:

答案 0 :(得分:0)

简而言之,您需要为models.FileField添加默认值。

这通常是因为原始数据库中有一些现有行并不包含file。因此,当您makemigrations时,db server无法确定此附加列的值。