Opened 5 years ago

Closed 5 years ago

#30795 closed Bug (invalid)

Django cannot serialize value into migration file.

Reported by: Sebastian Rossi Owned by: nobody
Component: Core (Serialization) Version: dev
Severity: Normal Keywords: Serialize, makemigrations, migrate
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi there i'm having problems when trying to use the command makemigrations with manage.py. The output is the following:

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

Traceback errors

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

The thing is that the entire app works just fine but for some reason i can't understand i cannot migrate the db.

Here is the models.py:

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"),
        ]

Any help is welcomed since i actually can't find what is wrong with the code. Not even in the documentation. I'm new with Django so maybe the solution is pretty easy but i can't find it.

Change History (1)

comment:1 by Mariusz Felisiak, 5 years ago

Resolution: invalid
Status: newclosed
Summary: Django cannot serialize value into migration fileDjango cannot serialize value into migration file.
Version: 2.2master
Note: See TracTickets for help on using tickets.
Back to Top