I'm having an issue that I seriously can't wrap my head around. I am using Django MPTT models and evrerything seems to be working fine (i.e. I can run the migrations and insert data in the database), but for some reason the
TreeForeignKey
table and the
TreeManyToManyField
table are not linking in the database.
Here are the models in question...
from mptt.models import MPTTModel, TreeForeignKey, TreeManyToManyField
class Category(MPTTModel):
name = models.CharField(
max_length=100,
verbose_name=_('category name'),
help_text=_('format: required, max=100'),
)
slug = models.SlugField(
max_length=150,
verbose_name=_('category safe URL'),
help_text=_('format: required, letters, numbers, underscore or hyphons'),
)
is_active = models.BooleanField(default=True)
parent = TreeForeignKey(
'self',
on_delete=models.PROTECT,
related_name='children',
null=True,
blank=True,
)
class MPTTMeta:
order_insertion_by = ['name']
class Meta:
verbose_name=_('product category')
verbose_name_plural=_('product categories')
def __str__(self):
return self.name
class Product(models.Model):
web_id = models.CharField(
unique=True,
max_length=50,
verbose_name=_('product website ID'),
)
slug = models.SlugField(
max_length=255,
verbose_name=_('product safe URL'),
help_text=_('format: required, letters, numbers, underscore or hyphons'),
)
name = models.CharField(max_length=150)
description = models.TextField(help_text='Required')
category = TreeManyToManyField(Category)
is_active = models.BooleanField(
default=False,
verbose_name=_('product visibility'),
)
created_at = models.DateTimeField(
editable=False,
auto_now_add=True,
help_text=_('format: Y-m-d H:M:S'),
)
updated_at = models.DateTimeField(auto_now=True, help_text=_('format: Y-m-d H:M:S'))
def __str__(self):
return self.name
I'm following the documentation verbatum, and really I have no idea why this is not working...
If anyone has any idea please let me know