【问题标题】:Django Model not saving to MySQL (Maria DB) while others doDjango 模型不保存到 MySQL (Maria DB) 而其他人保存
【发布时间】:2020-04-05 22:08:18
【问题描述】:

我的应用程序 (Django v 2.2.5) 正在使用 MySQL (MariaDB 10.4.6) 将数据存储在其数据库中,并已完成并提交了所有迁移。它创建了超级用户,当我登录时,我可以创建所有相关的对象,但是 Product(可以说是最重要的)没有被创建。我尝试更改模型,甚至删除 BooleanField(仅与正在存储的模型不同的唯一字段)并应用迁移,但它仍然不起作用。我什至删除了整个数据库并重新开始,希望绕过任何棘手的迁移,但这对我仍然没有任何帮助。 这是我的models.py

class Partner(models.Model):
    name = models.CharField(max_length=150, blank=False, help_text="Enter the vendor name", verbose_name="Vendor name", unique=True)
    address = models.TextField(help_text="Enter the address of the vendor")
    formula = models.ForeignKey(Formula, on_delete = models.CASCADE)
    phone = PhoneNumberField(help_text="enter the vendor phone number")
    email = models.EmailField(max_length=254, help_text="Enter the vendor email address")
    photo = models.ImageField(upload_to="vendors")

    def __str__(self):
        return self.name
class ProductType(models.Model):
    name = models.CharField(max_length=150, blank=False, help_text="Enter the kind of product this is", verbose_name="Product type")

    def __str__(self):
        return self.name

class Product(models.Model):
    name = models.CharField(max_length=150, blank=False, help_text="Enter the name of the product", verbose_name="Product name")
    price = models.DecimalField(max_digits=12, decimal_places=5,\
         help_text="Enter the price of the product", \
             blank = False, default=0.0)
    product_type = models.ForeignKey(ProductType, on_delete = models.CASCADE)
    vendor = models.ForeignKey(Partner, on_delete = models.CASCADE)
    photo = models.ImageField(upload_to="products")


    def __str__(self):
        return self.vendor.name + ": " + self.name

我使用的任何字段或元数据参数是否不受支持?我对自己做错了什么感到迷茫。我尝试查看日志,但看不到任何明显的东西可以为我指明正确的方向(我承认我不擅长数据库)。 这是错误日志:

InnoDB: using atomic writes.
2019-12-11 21:29:02 0 [Note] InnoDB: Mutexes and rw_locks use Windows interlocked functions
2019-12-11 21:29:02 0 [Note] InnoDB: Uses event mutexes
2019-12-11 21:29:02 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2019-12-11 21:29:02 0 [Note] InnoDB: Number of pools: 1
2019-12-11 21:29:02 0 [Note] InnoDB: Using SSE2 crc32 instructions
2019-12-11 21:29:02 0 [Note] InnoDB: Initializing buffer pool, total size = 16M, instances = 1, chunk size = 16M
2019-12-11 21:29:02 0 [Note] InnoDB: Completed initialization of buffer pool
2019-12-11 21:29:02 0 [Note] InnoDB: Starting crash recovery from checkpoint LSN=3370115
2019-12-11 21:29:03 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
2019-12-11 21:29:03 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1"
2019-12-11 21:29:03 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2019-12-11 21:29:03 0 [Note] InnoDB: Setting file 'C:\xampp\mysql\data\ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2019-12-11 21:29:03 0 [Note] InnoDB: File 'C:\xampp\mysql\data\ibtmp1' size is now 12 MB.
2019-12-11 21:29:03 0 [Note] InnoDB: Waiting for purge to start
2019-12-11 21:29:03 0 [Note] InnoDB: 10.4.6 started; log sequence number 3370124; transaction id 2759
2019-12-11 21:29:03 0 [Note] InnoDB: Loading buffer pool(s) from C:\xampp\mysql\data\ib_buffer_pool
2019-12-11 21:29:03 0 [Note] Plugin 'FEEDBACK' is disabled.
2019-12-11 21:29:03 0 [Note] Server socket created on IP: '::'.

软件版本:

  • Python 3.6.4(v3.6.4:d48eceb,2017 年 12 月 19 日,06:54:40)[MSC v.1900 64 位(AMD64)]
  • Django v 2.2.5
  • MariaDB 10.4.6
  • mysqlclient 1.4.4

如果您能给予我任何帮助,我将不胜感激。

【问题讨论】:

  • “我点击保存但什么也没做”是什么意思?没有错误,没有页面重新加载?如果是这样,问题不在于您的数据库,而在于一个或多个字段中的无效数据
  • 我的意思是当我点击保存按钮时它会加载但永远不会终止。我尝试输入无效数据进行控制,它向我显示了一条错误消息,但是当我修复错误时,它只是无限加载。

标签: python mysql django mariadb mariadb-10.4


【解决方案1】:

不久前我遇到了类似的问题。问题出在 MariaDB 10.4 上。

很奇怪,可以从 django 应用程序的任何位置保存数据,但管理页面不起作用。例如,做一个

恢复到 MariaDB 5.5 解决了这个问题。

【讨论】:

  • 奇怪的是,该应用程序开始自行保存。我不记得改变了什么。我所做的只是添加了保存前和保存后的信号接收器,看看东西在哪里中断,然后它突然开始工作了……电脑很奇怪。
【解决方案2】:

这些换行符在您的实际 Python 代码中吗?我怀疑他们是,但如果是这样,他们不应该是。

【讨论】:

  • 其实可以的。这是制作多行语句的方法之一,即使没有,它也会通过在 python 中调用异常来阻止服务器运行。它不会影响数据库
  • 我不确定,如果我以相同的方式将它们放在我的 django 模型中,它会为我抛出一个无效的语法错误。
  • @Pure-Entropy 试试Black,它将帮助您编写更清晰的代码。
  • 换行符不太可能是原因,但我会尝试删除它们。这一切都在 sqlite 上工作,所以我想知道为什么 mariadb 如此挑剔
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-13
  • 2020-08-01
  • 2012-03-27
  • 2020-11-09
  • 1970-01-01
相关资源
最近更新 更多