I made a relationships as follows on products and categories.
class Category(models.Model):
DoesNotExist = None
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(verbose_name='category name', max_length=50)
slug = models.SlugField()
parent = models.ForeignKey('self', blank=True, null=True, related_name='children', on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __str__(self):
full_path = [self.name]
k = self.parent
while k is not None:
full_path.append(k.name)
k = k.parent
return ' -> '.join(full_path[::-1])
class Product(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
product_name = models.CharField(verbose_name='product_name', null=False, max_length=200)
product_description = models.TextField(verbose_name='product description', null=False, max_length=1000)
stock = models.IntegerField(verbose_name='stock', null=False)
expire_date = models.DateTimeField(verbose_name='expire date')
company = models.ForeignKey(Company, on_delete=models.CASCADE)
store = models.ForeignKey(Store, blank=True, null=True, on_delete=models.SET_NULL)
category = models.ForeignKey(Category, blank=True, null=True, on_delete=models.SET_NULL)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __str__(self):
return self.product_name
def get_cat_list(self):
k = self.category # for now ignore this instance method
breadcrumb = ["dummy"]
while k is not None:
breadcrumb.append(k.slug)
k = k.parent
for i in range(len(breadcrumb) - 1):
breadcrumb[i] = '/'.join(breadcrumb[-1:i - 1:-1])
return breadcrumb[-1:0:-1]
So is there a functionality in django whereby if I have a relationship with a class on multiple models, I can take over all the data I have a relationship with?
Something like that in laravel?