Django:ValueError:执行makemigrations时,要解包的值太多(应为2)

pdkcd3nj  于 2023-04-13  发布在  Go
关注(0)|答案(1)|浏览(133)

当我尝试在我的django应用程序上执行makemigrations时,我得到了下面的错误:错误

Traceback (most recent call last):
  File "D:\GitHub\Laurant-Bookstore-2\manage.py", line 22, in <module>
    main()
  File "D:\GitHub\Laurant-Bookstore-2\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "D:\GitHub\Laurant-Bookstore-2\venv\Lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "D:\GitHub\Laurant-Bookstore-2\venv\Lib\site-packages\django\core\management\__init__.py", line 420, in execute
    django.setup()
  File "D:\GitHub\Laurant-Bookstore-2\venv\Lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "D:\GitHub\Laurant-Bookstore-2\venv\Lib\site-packages\django\apps\registry.py", line 116, in populate
    app_config.import_models()
  File "D:\GitHub\Laurant-Bookstore-2\venv\Lib\site-packages\django\apps\config.py", line 269, in import_models
    self.models_module = import_module(models_module_name)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "D:\GitHub\Laurant-Bookstore-2\app\livraria\models.py", line 40, in <module>
    class Carrinho(models.Model):
  File "D:\GitHub\Laurant-Bookstore-2\app\livraria\models.py", line 44, in Carrinho
    name, path, args, kwargs = super().deconstruct()
                               ^^^^^^^^^^^^^^^^^^^^^
  File "D:\GitHub\Laurant-Bookstore-2\venv\Lib\site-packages\django\db\models\fields\related.py", line 679, in deconstruct
    app_label, model_name = self.remote_field.model.split(".")
    ^^^^^^^^^^^^^^^^^^^^^
ValueError: too many values to unpack (expected 2)

模型.PY

from django.db import models
from django.contrib.auth.models import User

class GeneroLiterario(models.Model):
  nome = models.CharField(max_length=20)
  
  def __str__(self) -> str:
    return self.nome

class Autor(models.Model):
  nome = models.CharField(max_length=30, blank=False, null=False)
  foto = models.ImageField(upload_to="autores/%d-%b")
  biografia = models.TextField(max_length=2000, blank=False, null=False)
  nascimento = models.DateField(verbose_name="Data de nascimento", blank=False, null=False)
  morte = models.DateField(verbose_name="Data de morte", blank=True, null=True, help_text="Caso ainda vivo, deixar em branco")
  genero_literario = models.ForeignKey(GeneroLiterario, on_delete=models.SET_NULL, null=True, blank=True)
  frase_destaque = models.TextField(verbose_name="Frase exibida no perfil", max_length=200, blank=True, null=True)

  def __str__(self) -> str:
    return self.nome

class Livro(models.Model):
  titulo = models.CharField(max_length=100, blank=False, null=False)
  autor = models.ForeignKey(Autor, on_delete=models.SET_NULL, null=True, blank=False)
  preco = models.DecimalField(max_digits=10, decimal_places=2, blank=False, null=False)
  desconto = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
  sinopse = models.TextField(max_length=2000, blank=False, null=False)
  genero_literario = models.ForeignKey(GeneroLiterario, on_delete=models.SET_NULL, null=True, blank=True)
  capa = models.ImageField(upload_to="livros/%d-%b",null=False, blank=False)
  
  def __str__(self) -> str:
    return self.titulo
  

class Carrinho(models.Model):
  usuario = models.ForeignKey(User, blank=True, on_delete=models.CASCADE, null=True,)
  livro = models.ManyToManyField(Livro, blank=True)
  
  def __str__(self) -> str:
    return self.livro.titulo

我尝试在我的数据库中创建一个新表来创建一个简单的购物车(类Carrinho),但是当我尝试执行makemigration时,我得到了这个错误,有人知道它是什么吗?
(代码中的变量、函数和类名都是葡萄牙语)

w8ntj3qf

w8ntj3qf1#

在类'Carrinho'中函数中的错误,您尝试访问ManytoMany reletationShip的titulo属性将您的函数更新到此

def __str__(self) -> str:
    return ", ".join([livro.titulo for livro in self.livro.all()])

相关问题