django:如何从查询集中延迟TextField和字段的本地化变体,包括相关模型

axkjgtzd  于 2023-05-30  发布在  Go
关注(0)|答案(1)|浏览(77)

当对具有大文本字段的相关模型进行查询时,django将所有数据加载到SQL查询中。
如何从主模型和查询集中自动延迟文本字段?
如何从查询字段(如description、description_)中排除??,text,text_??
选择DISTINCT store_itemidstore_itemstore_id,..... store_itemproduct_idstore_itemprice_instore_itemprice_outstore_itemcountproductidproductnameproductname_ukproductname_ruproductname_enproductmanufacturer_id,... product . descriptionproductdescription_ukproductdescription_ruproductdescription_enproducttextproducttext_ukproducttext_ruproducttext_en,... product_manufacturer . idproduct_manufactureraction_idproduct_manufacturernameproduct_manufacturerslugproduct_manufacturercode,... product_manufacturer . descriptionproduct_manufacturerdescription_ukproduct_manufacturerdescription_ruproduct_manufacturerdescription_en,... product_manufacturer . textproduct_manufacturertext_ukproduct_manufacturertext_ruproduct_manufacturertext_en,...从store_item内接缝productstore_itemproduct_id = productid左外接合product_manufacturerproduct上。manufacturer_id = product_manufacturerid ORDER BY product_manufacturer . name ASC,productname ASC
选择DISTINCT store_itemidstore_itemstore_id,..... store_itemproduct_idstore_itemprice_instore_itemprice_outstore_itemcountproductidproductnameproductname_ukproductname_ruproductname_enproductmanufacturer_id,..... product_manufacturer . idproduct_manufactureraction_idproduct_manufacturernameproduct_manufacturerslugproduct_manufacturercode,从store_item内接缝productstore_itemproduct_id = productid左外接合product_manufacturerproduct上。manufacturer_id = product_manufacturerid ORDER BY product_manufacturer . name ASC,productname ASC

lf5gs5x2

lf5gs5x21#

下面是一个可以用来延迟查询大字段的函数。它将收集所有文本字段,包括本地化的变体,可能有人发现它有用。排除参数也存在。你可以像这样使用它:
items = queryset.defer(*get_related_fields(queryset))

from django.conf import settings
from django.db.models import TextField
import re

def get_related_fields(related_fields, prefix=''):
    for field, subfields in related_fields.items():
        yield prefix + field
        if subfields:
            yield from get_related_fields(subfields, prefix + field + '__')

def get_related_model(model, related_field):
    for field in related_field.split('__'):
        model = getattr(model, field).field.related_model
    return model

def get_text_fields_with_related(queryset, exclude=None, fields=(TextField, )):
    text_fields = []
    exclude_list = exclude if exclude else []
    locales = '|'.join(dict(settings.LANGUAGES).keys())
    for field in queryset.model._meta.get_fields():
        if isinstance(field, fields):
            for exclude in exclude_list:
                if re.match(r'^%s(_(%s))?$' % (exclude, locales), field.name):
                    break
            else:
                text_fields.append(field.name)
    for related_field in get_related_fields(queryset.query.select_related):
        related_obj = get_related_model(queryset.model, related_field)
        for field in related_obj._meta.get_fields():
            if isinstance(field, fields):
                for exclude in exclude_list:
                    if re.match(r'^%s(_(%s))?$' % (exclude, locales), field.name):
                        break
                else:
                    text_fields.append(f"{related_field}__{field.name}")
    return text_fields

相关问题