我来到这里是因为我有一个模型,其中有一个指向这个模型“CampaignsProducts”的模型,它的str属性正在生成许多查询:
前 :
class CampaignsProducts(mdlcom.CommonStructure):
product = models.ForeignKey(
'products.Product',
verbose_name=u'Producto',
null=False, on_delete=models.CASCADE,
related_name="%(app_label)s_%(class)s_product_set",
)
campaign = models.ForeignKey(
'prices.Campaigns',
verbose_name=u'Campaña',
null=False, on_delete=models.CASCADE,
related_name="%(app_label)s_%(class)s_campaign_set",
)
def __str__(self):
return '{} - {}'.format(self.product.name, self.campaign.name)
为了修复它,我更改了以下“ str ”属性
def __str__(self):
return str(self.id)
Mikko 提供的解决方案挽救了我的生命:
class CouponForm(forms.ModelForm):
NO_REQUIRED = [
'sellers', 'platforms', 'campaigns_product',
]
class Meta:
model = mdlpri.Coupon
fields = '__all__'
def get_campaigns_choices(self):
qscampaigns = (
mdlpri.CampaignsProducts.objects.all()
.select_related("product")
.select_related("campaign")
.order_by("id")
)
choices = []
for item in qscampaigns:
wdescription = '{} - {}'.format(
item.product.name, item.campaign.name
)
choices.append((item.id, wdescription))
return choices
def __init__(self, *args, **kwargs):
super(CouponForm, self).__init__(*args, **kwargs)
for wfield in self.NO_REQUIRED:
self.fields[wfield].required = False
个性化选择
w = self.fields["campaigns_product"].widget
w.choices = self.get_campaigns_choices()
变更前查询(按产品):
SELECT "products_product"."id", "products_product"."name", "products_product"."custom", "products_product"."created_at", "products_product"."updated_at", "products_product"."active", "products_product"."position", "products_product"."url", "products_product"."for_bonus_platform", "products_product"."for_cms_platform", "products_product"."for_store_platform", "products_product"."for_call_center_platform", "products_product"."for_ibk_platform", "products_product"."for_scotiabank_platform", "products_product"."for_bbva_platform", "products_product"."for_refurbished_platform" FROM "products_product" WHERE "products_product"."id" = 1
更改后查询:
SELECT "prices_campaignsproducts"."id", "prices_campaignsproducts"."date_created_at", "prices_campaignsproducts"."created_at", "prices_campaignsproducts"."updated_at", "prices_campaignsproducts"."user_id", "prices_campaignsproducts"."last_user_modified_id", "prices_campaignsproducts"."product_id", "prices_campaignsproducts"."campaign_id", "prices_campaignsproducts"."price", "prices_campaignsproducts"."is_active", "prices_campaignsproducts"."expiration_from", "prices_campaignsproducts"."expiration_to", "products_product"."id", "products_product"."name", "products_product"."custom", "products_product"."created_at", "products_product"."updated_at", "products_product"."active", "products_product"."position", "products_product"."url", "products_product"."for_bonus_platform", "products_product"."for_cms_platform", "products_product"."for_store_platform", "products_product"."for_call_center_platform", "products_product"."for_ibk_platform", "products_product"."for_scotiabank_platform", "products_product"."for_bbva_platform", "products_product"."for_refurbished_platform", "prices_campaigns"."id", "prices_campaigns"."date_created_at", "prices_campaigns"."created_at", "prices_campaigns"."updated_at", "prices_campaigns"."user_id", "prices_campaigns"."last_user_modified_id", "prices_campaigns"."name", "prices_campaigns"."is_active" FROM "prices_campaignsproducts" INNER JOIN "products_product" ON ("prices_campaignsproducts"."product_id" = "products_product"."id") INNER JOIN "prices_campaigns" ON ("prices_campaignsproducts"."campaign_id" = "prices_campaigns"."id") ORDER BY "prices_campaignsproducts"."id" ASC
只是想分享我的经验。
最好的祝福,