P
P
PiggyPig2017-07-20 02:51:42
Django
PiggyPig, 2017-07-20 02:51:42

How many models and relationships will have to be created?

I will say right away that I have only recently begun to get acquainted with Django (a priori the code will not be so hot). I hope for adequate help or at least some reasonable comments. Thanks in advance!) By the way, Django version is 1.11, Python 3.6.
The situation is this. When developing the store, I got confused in the models. You need to create, in other words, such a path in the URL:
domain/section/subsection/page.
Complicating everything is that you want to make nesting names not based on their id in the database. But I didn’t think of how to set their own URl for them and at the same time not write each link in urls.py (there are quite a lot of them). This is one side of the coin.
The second turns out to be in the links between the models. I will try to build a diagram more clearly + drawing.
62be3d8046e84855a0514e04492ad5d9.png
Take, for example, two sections: fruits and vegetables. In fruits, the subsections are apples and oranges, and in vegetables, cucumbers and tomatoes. All subsections have pages for their varieties, as well as pages for hybrids that appear in both subsections of mixed products and have links to each of these subsections (such as tags or something like that). As for nesting, that's all.
There is also a "list" of the best varieties of the product, which is displayed on the pages of varieties that are in this list.
I tried to explain as clearly as possible, although I felt that it was rubbish.
After reading the documentation about the many-to-one, many-to-many and one-to-one relationships, as well as about some of their limitations, to put it mildly, I fell into a stupor. In models.py I wrote the following models in accordance with how I understood:

from django.db import models


# Разделы (фрукты, овощи)
class Fruit_or_vegetable(models.Model):
    name = models.CharField(max_length=20)

    def __str__(self):
        return self.name


# Подразделы (яблоки, апельсины, помидоры, )
class Product(models.Model):
    name = models.CharField(max_length=20)
    fruit_or_vegetable = models.ForeignKey(Fruit_or_vegetable, on_delete=models.CASCADE)
    sort = models.ForeignKey(Sort, on_delete=models.CASCADE)
    best = models.ForeignKey(Best, on_delete=models.CASCADE)

    def __str__(self):
        return self.name


# Страницы (сорта и гибриды)
class Sort(models.Model):
    name = models.CharField(max_length=20)
    two_product = models.ManyToManyField(Product, through='Hybrid')

    def __str__(self):
        return self.name


# Гибриды (два продукта)
class Hybrid(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    sort = models.ForeignKey(Sort, on_delete=models.CASCADE)


# Лучшие сорта (один продукт) P.S. Гибриды тоже могут включаться в список
class Best(models.Model):
    product = models.ManyToManyField(Sort, though='Product')
    why = models.TextField(max_length=50)

Something like this. I hope it can be disassembled.
If this is the correct course of action, are there any errors?
Maybe you don’t need to write such dependencies at all and you can get by with simpler solutions.
In any case, thanks for your attention!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Abdulla Mursalov, 2017-07-20
@amaprograma

class Category(models.Model):
    parent = models.ForeignKey('self', null=True)
    title = models.CharField(...)

class Product(models.Model):
    title = models.CharField(...)

Apple Golden, this is a product of the category Fruit, subcategory Apple, subsubcategory Golden.
A hybrid is a subcategory that has two parent categories. Hence: The best sort is just a boolean value ->parent = models.ManyToManyField('self', null=True)
# in class Category
is_best = models.Boolean(default=False)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question