嗨,我在尝试将命令 makemigrations 与 manage.py 一起使用时遇到问题。输出如下:
Migrations for 'reg':
reg\migrations\0012_auto_20190917_1711.py
- Remove field Products from tenant
- Add field products to tenant
- Alter field productos on preaprobado
- Alter field tenant_id on preaprobado
- Alter field CMS_id on producto
- Alter field CMS_id on tenant
回溯错误
ValueError: Cannot serialize: <Producto: Basico - ['IPTEL', 'Rocstar', 'Test_DVT']>
There are some values Django cannot serialize into migration files.
For more, see https://docs.djangoproject.com/en/2.2/topics/migrations/#migration-serializing
问题是整个应用程序运行良好,但由于某种原因我无法理解我无法迁移数据库。
这是models.py:
class Producto(models.Model):
Name = models.CharField(max_length = 50, unique = True)
CMS_id = models.PositiveIntegerField(unique = True)
def __str__(self):
#Toma los tenants que tienen el producto asignado por el nombre y forma una lista de los nombres de los tenants
#para que aparezcan al lado del nombre del producto
tenants_with_product = Tenant.objects.filter(products__Name = self.Name)
tenants_dict = tenants_with_product.in_bulk(field_name = "Name")
tenants_list = []
for tenant_name in tenants_dict.keys():
tenants_list.append(tenant_name)
return(self.Name + " - " + tenants_list.__str__())
class Tenant(models.Model):
Name = models.CharField(max_length = 30, unique = True)
Enabled = models.BooleanField(default = False)
CMS_id = models.PositiveIntegerField(unique = True)
Logo = models.ImageField(upload_to = "Logos/", blank = True)
Icon = models.ImageField(upload_to = "Icons/", blank = True)
Domain = models.URLField(default = "http://localhost:8000")
Master = models.BooleanField(default = False)
products = models.ManyToManyField(Producto, related_name = "tenants")
def __str__(self):
return(self.Name)
# Override del metodo que salva en la db para que si se quiere salvar un Tenant con Master = True y
# existe un Tenant con mismo dominio y Master = True no pueda salvar
def save(self, *args, **kwargs):
#busca tenant con mismo dominio y Master = True
try:
master_tenant = Tenant.objects.filter(Domain = self.Domain).get(Master = True)
#Si el tenant encontrado es el que se quiere salvar chequeo que sea Master = True y salvo
#sino pido Master = True. Chequea tambien que el master tenga logo e icono si o si
if master_tenant.id == self.id:
if self.Master:
if self.Logo != "" and self.Icon != "":
super(Tenant, self).save(*args, **kwargs)
else:
raise ValidationError({"Logo" : "Logo required", "Icon" : "Icon rquired"})
else:
raise ValidationError({"Master" : "At least one Master per Domain"})
else:
#Si no es el Master = True del dominio y Master = False guarda, sino error
if self.Master:
if self.Logo != "" and self.Icon != "":
super(Tenant, self).save(*args, **kwargs)
else:
raise ValidationError({"Logo" : "Logo required", "Icon" : "Icon rquired"})
else:
super(Tenant, self).save(*args, **kwargs)
#Si no existe tenant con mismo Domain y Master = True, Se fija que Master = True y que tenga logo e icono y salva, sino, error
except Tenant.DoesNotExist:
if self.Master:
if self.Logo != "" and self.Icon != "":
super(Tenant, self).save(*args, **kwargs)
else:
raise ValidationError({"Logo" : "Logo required", "Icon" : "Icon rquired"})
else:
raise ValidationError({"Master" : "At least one Master per Domain"})
class Preaprobado(models.Model):
codigo = models.CharField(max_length = 100)
tenant_id = models.ForeignKey(Tenant, on_delete = models.CASCADE, related_name = "tenant")
can_register = models.BooleanField(default = True)
is_registered = models.BooleanField(default = False)
productos = models.ManyToManyField(Producto, related_name = "preaprobados", default = Producto.objects.get(id = 1))
def __str__(self):
return(self.codigo + "-" + self.tenant_id.Name)
class Meta():
#No permite un par codigo-tenant repetido dentro del modelo Preaprobado
constraints = [
models.UniqueConstraint(fields = ["codigo", "tenant_id"], name = "par código-Tenant únicos"),
]
欢迎任何帮助,因为我实际上找不到代码有什么问题。甚至在文档中也没有。我是 Django 新手,所以解决方案可能很简单,但我找不到。