B
B
blackbb2022-04-09 14:27:05
Django
blackbb, 2022-04-09 14:27:05

How to build an object model correctly?

There is a model called "Distances", it shows how far the object is from the hotel and on foot or by car. For example, the Kasel hotel has such distances as a market, an embankment, a pharmacy, etc. attached to it. (There are about 15 such objects). To the market, for example, 10 minutes on foot, to the pharmacy 15 minutes by car. How to build connections and models correctly so as not to overwhelm the base, since there will be many hotels.

class Hotel(models.Model):
    user = models.ForeignKey(MyUser, on_delete=models.CASCADE, verbose_name='Владелец')
    title = models.CharField(max_length=100, verbose_name='Название')

class Distance(models.Model):
    title = models.CharField(max_length=100, verbose_name='Название')
    hotel = models.ManyToManyField(Hotel, blank=True, verbose_name='Гостиница')

METHODCHOICE = (
    ('walk', ('Пешком')),
    ('avto', ('На авто')),
)

class DistanceTime(models.Model):
    time = models.IntegerField(verbose_name='Время', blank=True, null=True)
    method = models.CharField(max_length=10, choices=METHODCHOICE, blank=True, null=True, verbose_name='Метод')
    distance = models.ForeignKey(Distance, on_delete=models.CASCADE, verbose_name='Объект')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2022-04-09
@blackbb

a popular template has become, Extra fields on many-to-many relationships https://docs.djangoproject.com/en/4.0/topics/db/mo...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question