将字段添加到现有模型

时间:2015-08-01 20:41:21

标签: django file-upload django-models django-forms

我想在我的ClientUploads模型中添加created_at字段。它看起来像这样:

@python_2_unicode_compatible
class ClientUploads(models.Model):

    client = models.OneToOneField(Client)
    #created_at = models.DateTimeField(auto_now_add=True)

    def generate_filename(self, filename):
        name = "uploads/%s/%s" % (self.client.company, filename)
        return name

    file_upload = models.FileField(upload_to=generate_filename)

    def __str__(self):
        return self.client.company

    class Meta:
        verbose_name_plural = _("Client Uploads")
        verbose_name = _("Client Upload")

但是当我添加created_at然后提交表单时,我得到的错误是该列不存在。我尝试重命名模型,并进行迁移以擦除旧模型并创建另一个模型,其中created_at从一开始就存在但我仍然遇到相同的错误。如何添加此字段?或者是否已经有这样的领域?无论我需要一个created_at字段,我都无法在不破坏表单的情况下找到它的添加方法。

2 个答案:

答案 0 :(得分:0)

根据您发布的代码,您已将created_at字段注释掉。您需要取消注释该行(删除前面的using System; using System.IO; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { var fileContents = ReadFile(); Console.ReadLine(); // cause program to pause at the end } public static string ReadFile() { try { using (var streamReader = new StreamReader( @"C:\MyTestFile.txt")) { var fileContents = streamReader.ReadToEnd(); Console.WriteLine("File was read successfully"); return fileContents; } } catch (FileNotFoundException fileNotFoundException) { LogReadFileException(fileNotFoundException); } catch (DirectoryNotFoundException directoryNotFoundException) { LogReadFileException(directoryNotFoundException); } catch (IOException ioException) { LogReadFileException(ioException); } // If we get here, an exception occurred Console.WriteLine("File could not be read successfully"); return string.Empty; } private static void LogReadFileException(Exception exception) { string logFilePath = @"C:\MyLogFile.txt"; using (var streamWriter = new StreamWriter(logFilePath, append: true)) { var errorMessage = "Exception occurred: " + exception.Message; streamWriter.WriteLine(errorMessage); Console.WriteLine(errorMessage); } } } } ),然后运行#,然后manage.py makemigrations

答案 1 :(得分:0)

删除数据库,然后运行makemigrations并migrate和createsuperuser,并从头开始重新输入数据。我试过冲洗,没有工作。 echo "" > dbfile也有效。