Answer the question
In order to leave comments, you need to log in
What is the best way to organize models relationships in Django?
Please help, how best to organize the links between the models?
I tried several options, but somehow it didn’t work out well
. The main model through which everything is displayed is SparePart, the rest is as an addition
from django.db import models
from django.utils import timezone
class SparePart(models.Model):
name_part = models.CharField(verbose_name='Имя запчасти', max_length=120)
number_part = models.CharField(verbose_name='Номер запчасти', max_length=70, blank=True, null=True)
text_info = models.TextField(blank=True, null=True, verbose_name='Описание')
pub_date = models.DateTimeField(verbose_name='Дата публикации')
car_part = models.ManyToManyField("ModelCar", verbose_name='Модель')
modification_car = models.ManyToManyField('ModificationCar', verbose_name='Модификация')
engine_car = models.ManyToManyField('EngineCar', verbose_name='Двигатель')
main_image = models.ImageField(upload_to='product_image/main/', blank=True, null=True)
spare_image = models.ManyToManyField('ImageProduct', verbose_name='Изображение')
type_spare_part = models.ForeignKey('TypeSparePart', null=True, verbose_name='Тип Запчасти', on_delete=models.CASCADE)
def __str__(self):
return self.name_part
class Meta:
verbose_name = "Запчасть"
verbose_name_plural = "Запчасти"
class MakeCar(models.Model):
name_make_car = models.CharField(verbose_name='Производитель', max_length=100)
def __str__(self):
return self.name_make_car
class Meta:
verbose_name = "Производитель"
verbose_name_plural = "Производители"
class ModelCar(models.Model):
name_model_car = models.CharField(verbose_name='Модель', max_length=100)
car_model = models.ForeignKey("MakeCar", verbose_name='Производитель', on_delete=models.CASCADE)
def __str__(self):
return '{} - {}'.format(self.car_model.name_make_car, self.name_model_car)
class Meta:
verbose_name = "Модель"
verbose_name_plural = "Модели"
class ModificationCar(models.Model):
modification_car = models.CharField(max_length=50, verbose_name='Модификация')
def __str__(self):
return str(self.modification_car)
class Meta:
verbose_name = "Модификация Авто"
verbose_name_plural = "Модификации Авто"
class TypeSparePart(models.Model):
name_type = models.CharField(max_length=100)
def __str__(self):
return str(self.name_type)
class Meta:
verbose_name = "Тип Запчасти"
verbose_name_plural = "Тип Запчасти"
class EngineCar(models.Model):
name_engine = models.CharField(max_length=100)
def __str__(self):
return str(self.name_engine)
class Meta:
verbose_name = "Двигатель"
verbose_name_plural = "Двигателя"
class ImageProduct(models.Model):
image_product = models.ImageField(upload_to='product_image/', blank=True, null=True)
def __str__(self):
return str(self.image_product)
class Meta:
verbose_name = "Изображение"
verbose_name_plural = "Изображения"
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question