I’m developing an online store using Django. I have a main class Product
<code>class Product(models.Model):
our_code = models.CharField('Наш артикул', max_length = 255, unique=True, blank=True, null=True)
name = models.CharField('Наименование',max_length = 255, blank=True, null=True)
full_name = models.CharField('Полное наименование',max_length = 255, blank=True, null=True)
('manual', 'Ручная установка'),
CURRENCY_TYPE_CHOICES = [
price_type = models.CharField('Тип цены',max_length=10, choices=PRICE_TYPE_CHOICES, default='manual')
manual_price = models.DecimalField('Ручная цена',max_digits=20, decimal_places=2, blank=True, null=True)
rrc_price = models.DecimalField('РРЦ', max_digits=20, decimal_places=2, blank=True, null=True)
mrc_price = models.DecimalField('МРЦ', max_digits=20, decimal_places=2, blank=True, null=True)
currency = models.CharField('Валюта',max_length=10, choices=CURRENCY_TYPE_CHOICES, default='rub')
price_currency = models.DecimalField('Цена в валюте', max_digits=20, decimal_places=2, blank=True, null=True)
text_short = models.TextField('Краткое описание', blank=True, null=True)
text_full = models.TextField('Полное описание', blank=True, null=True)
<code>class Product(models.Model):
our_code = models.CharField('Наш артикул', max_length = 255, unique=True, blank=True, null=True)
name = models.CharField('Наименование',max_length = 255, blank=True, null=True)
full_name = models.CharField('Полное наименование',max_length = 255, blank=True, null=True)
PRICE_TYPE_CHOICES = [
('manual', 'Ручная установка'),
('rrc', 'РРЦ'),
('mrc', 'МРЦ'),
]
CURRENCY_TYPE_CHOICES = [
('rub', 'RUB (₽)'),
('usd', 'USD ($)'),
('eur', 'EUR (€)'),
('gbr', 'GBP (£)'),
]
price_type = models.CharField('Тип цены',max_length=10, choices=PRICE_TYPE_CHOICES, default='manual')
manual_price = models.DecimalField('Ручная цена',max_digits=20, decimal_places=2, blank=True, null=True)
rrc_price = models.DecimalField('РРЦ', max_digits=20, decimal_places=2, blank=True, null=True)
mrc_price = models.DecimalField('МРЦ', max_digits=20, decimal_places=2, blank=True, null=True)
currency = models.CharField('Валюта',max_length=10, choices=CURRENCY_TYPE_CHOICES, default='rub')
price_currency = models.DecimalField('Цена в валюте', max_digits=20, decimal_places=2, blank=True, null=True)
text_short = models.TextField('Краткое описание', blank=True, null=True)
text_full = models.TextField('Полное описание', blank=True, null=True)
</code>
class Product(models.Model):
our_code = models.CharField('Наш артикул', max_length = 255, unique=True, blank=True, null=True)
name = models.CharField('Наименование',max_length = 255, blank=True, null=True)
full_name = models.CharField('Полное наименование',max_length = 255, blank=True, null=True)
PRICE_TYPE_CHOICES = [
('manual', 'Ручная установка'),
('rrc', 'РРЦ'),
('mrc', 'МРЦ'),
]
CURRENCY_TYPE_CHOICES = [
('rub', 'RUB (₽)'),
('usd', 'USD ($)'),
('eur', 'EUR (€)'),
('gbr', 'GBP (£)'),
]
price_type = models.CharField('Тип цены',max_length=10, choices=PRICE_TYPE_CHOICES, default='manual')
manual_price = models.DecimalField('Ручная цена',max_digits=20, decimal_places=2, blank=True, null=True)
rrc_price = models.DecimalField('РРЦ', max_digits=20, decimal_places=2, blank=True, null=True)
mrc_price = models.DecimalField('МРЦ', max_digits=20, decimal_places=2, blank=True, null=True)
currency = models.CharField('Валюта',max_length=10, choices=CURRENCY_TYPE_CHOICES, default='rub')
price_currency = models.DecimalField('Цена в валюте', max_digits=20, decimal_places=2, blank=True, null=True)
text_short = models.TextField('Краткое описание', blank=True, null=True)
text_full = models.TextField('Полное описание', blank=True, null=True)
Associated with it is the ProductWith class, which stores similar products and is displayed in the Product class in the admin panel
<code>class ProductWith(models.Model):
product = models.ForeignKey(Product, related_name='primary_product_with', default=None, on_delete=models.CASCADE, verbose_name='Товар')
product_with = models.ForeignKey(Product, related_name='product_with_products_with', default=None, on_delete=models.CASCADE, verbose_name='Советуем купить')
<code>class ProductWith(models.Model):
product = models.ForeignKey(Product, related_name='primary_product_with', default=None, on_delete=models.CASCADE, verbose_name='Товар')
product_with = models.ForeignKey(Product, related_name='product_with_products_with', default=None, on_delete=models.CASCADE, verbose_name='Советуем купить')
</code>
class ProductWith(models.Model):
product = models.ForeignKey(Product, related_name='primary_product_with', default=None, on_delete=models.CASCADE, verbose_name='Товар')
product_with = models.ForeignKey(Product, related_name='product_with_products_with', default=None, on_delete=models.CASCADE, verbose_name='Советуем купить')
code admin.py
<code>class ProductWithAdmin(admin.TabularInline):
raw_id_fields = ('product_with',)
autocomplete_fields = ('product_with',)
<code>class ProductWithAdmin(admin.TabularInline):
model = ProductWith
fk_name = 'product'
extra = 0
raw_id_fields = ('product_with',)
autocomplete_fields = ('product_with',)
</code>
class ProductWithAdmin(admin.TabularInline):
model = ProductWith
fk_name = 'product'
extra = 0
raw_id_fields = ('product_with',)
autocomplete_fields = ('product_with',)
<code>class ProductAdmin(admin.ModelAdmin):
inlines = (ProductProviderAdmin, ProductImageAdmin, ProductAnalogAdmin, ProductColorAdmin, ProductWithAdmin,)
search_fields = ['name', 'our_code']
autocomplete_fields = ["category", "manufacturer", "provider"]
<code>class ProductAdmin(admin.ModelAdmin):
inlines = (ProductProviderAdmin, ProductImageAdmin, ProductAnalogAdmin, ProductColorAdmin, ProductWithAdmin,)
search_fields = ['name', 'our_code']
autocomplete_fields = ["category", "manufacturer", "provider"]
</code>
class ProductAdmin(admin.ModelAdmin):
inlines = (ProductProviderAdmin, ProductImageAdmin, ProductAnalogAdmin, ProductColorAdmin, ProductWithAdmin,)
search_fields = ['name', 'our_code']
autocomplete_fields = ["category", "manufacturer", "provider"]
The problem is that when you open a product in the admin panel that has ProductWith, similar queries occur (exactly as many as there are products)
i tried using select_related and prefetch_related but nothing helps