Django 加载非常慢



我是 Django 的新手。 我的 Web 应用程序加载的一页非常慢。我不知道瓶颈在哪里。

有我的代码:

model.py

class Fact_CarCase(models.Model):
CaseCode = models.IntegerField()
FraudulantRate = models.FloatField(null=True)
FraudType = models.ForeignKey('Dim_FraudType', on_delete=models.CASCADE, null=True)
IssueUnit = models.ForeignKey('Dim_Units', to_field="UnitCode", related_name='Fact_CarCase_IssueUnit', on_delete=models.CASCADE)
AgentUnit = models.ForeignKey('Dim_Units', to_field="UnitCode", related_name='Fact_CarCase_AgentUnit', on_delete=models.CASCADE)
IncidentCity = models.ForeignKey('Dim_City', to_field="CityCode", related_name='Fact_CarCase_IncidentCity', on_delete=models.CASCADE)
CaseUnit = models.ForeignKey('Dim_Units', to_field="UnitCode", related_name='Fact_CarCase_CaseUnit', on_delete=models.CASCADE)
CaseDate = models.ForeignKey('Dim_Date', to_field="MasterDate", related_name='Fact_CarCase_CaseDate', on_delete=models.CASCADE)
IssueDate = models.ForeignKey('Dim_Date', to_field="MasterDate", related_name='Fact_CarCase_IssueUnitDate', on_delete=models.CASCADE)
LastEndosmentDate = models.ForeignKey('Dim_Date', to_field="MasterDate", related_name='Fact_CarCase_LastEndosmentDate', on_delete=models.CASCADE)
DriverType = models.ForeignKey('Dim_DriverType', to_field="DriverTypeCode", related_name='Fact_CarCase_DriverType', on_delete=models.CASCADE, null=True)
CarKind = models.ForeignKey('Dim_CarKind', to_field="CarKindCode", related_name='Fact_CarCase_CarKind', on_delete=models.CASCADE, null=True)
CompleteCause = models.ForeignKey('Dim_CompleteCause', to_field="CompleteCauseCode", related_name='Fact_CarCase_CompleteCause', on_delete=models.CASCADE, null=True)
CaseInspector = models.ForeignKey('Dim_CaseInspector', to_field="CaseInspectorCode", related_name='Fact_CarCase_CaseInspector', on_delete=models.CASCADE, null=True)
slug = models.SlugField(max_length=100, null=True)
DraftAmount = models.BigIntegerField(null=True)
CaseOfUsage = models.ForeignKey('Dim_CaseOfUsage', to_field="CaseOfUsageCode", related_name='Fact_CarCase_CaseOfUsage', on_delete=models.CASCADE, null=True)
IncidentReason = models.ForeignKey('Dim_IncidentReason', to_field="IncidentReasonCode", related_name='Fact_CarCase_IncidentReason', on_delete=models.CASCADE, null=True)
CaseType = models.ForeignKey('Dim_CaseType', to_field="CaseTypeCode", related_name='Fact_CarCase_CaseType', on_delete=models.CASCADE)
ReviewItems = models.ManyToManyField('Dim_ReviewItems', through='Middle_CaseReviewItems')
FinalInsurerName = models.CharField(max_length=500, null=True)
IncidentKind = models.CharField(max_length=500, null=True)
IncidentDate = models.ForeignKey('Dim_Date', to_field="MasterDate", related_name='Fact_CarCase_IncidentDate', on_delete=models.CASCADE, null=True)
# FeedBack = models.ManyToManyField('Fact_FeedBack', through='Middle_CaseFeedBack')
def save(self, *args, **kwargs):
self.slug = self.slug or slugify(self.CaseCode)
super().save(*args, **kwargs)
def __str__(self):
# return self.CaseUnit.UnitTitle
return '{} در تاریخ {}'.format(self.CaseUnit.UnitTitle, self.CaseDate.MasterDate_Slash)
# ------------------------------------------------------------------------------
class Dim_Units(models.Model):
UnitCode = models.IntegerField(unique=True)
UnitTitle = models.CharField(max_length=500)
def __str__(self):
return self.UnitTitle
# ------------------------------------------------------------------------------
class Dim_IncidentReason(models.Model):
IncidentReasonCode = models.IntegerField(unique=True)
IncidentReasonTitle = models.CharField(max_length=500)
def __str__(self):
return self.IncidentReasonTitle

.
.
.

Filters.py

class CaseFilter(django_filters.FilterSet):
CaseDate_Year__gte = django_filters.NumberFilter(field_name='CaseDate', lookup_expr='MasterYear__gte')
CaseDate_Month__gte = django_filters.NumberFilter(field_name='CaseDate', lookup_expr='MasterMonth__gte')
CaseDate_Day__gte = django_filters.NumberFilter(field_name='CaseDate', lookup_expr='MasterDay__gte')
CaseDate_Year__lte = django_filters.NumberFilter(field_name='CaseDate', lookup_expr='MasterYear__lte')
CaseDate_Month__lte = django_filters.NumberFilter(field_name='CaseDate', lookup_expr='MasterMonth__lte')
CaseDate_Day__lte = django_filters.NumberFilter(field_name='CaseDate', lookup_expr='MasterDay__lte')
FraudulantRate__gte = django_filters.NumberFilter(field_name='FraudulantRate', lookup_expr='gte')
FraudulantRate__lte = django_filters.NumberFilter(field_name='FraudulantRate', lookup_expr='lte')
class Meta:
model = models.Fact_CarCase
fields = ['CaseType']

views.py

@login_required
def search(request):
case_list = models.Fact_CarCase.objects.all()
case_filter = filters.CaseFilter(request.GET, queryset=case_list)
return render(request, 'CarIA/user_list.html', {'filter': case_filter})

模板:


.
.
.
<div class="mypopup"  id="myForm">
<form method="get">
<div style="text-align:right;padding-right:8px;color:#303A43;font-size:14px;">فیلترها </div>
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<div>
<div>
<label class="quti"  style="position:absolute;top:36px;right:16px;font-size:12px;color: #92A6B6;">از تاریخ</label>
<div>
<input type="number" name="CaseDate_Day__gte" step="any" id="id_CaseDate_Day__gte"
style="top:58px;left:223px;position:absolute;width:47px;height:36px;border: 1px solid #CCDDEB;border-radius: 4px;opacity: 1;font-size:14px;
letter-spacing: 0;color: #303A43;opacity: 1;text-align: center;float:right;direction:rtl;">
</div>

<script>
var CaseDate_gte = document.getElementById("id_CaseDate_Year__gte").value
+document.getElementById("id_CaseDate_Month__gte").value
+document.getElementById("id_CaseDate_Day__gte").value;
</script>

<div style="width:60px;height:22px;color: #CCDDEB;font-size:19px;position:absolute;top:58px;left:210px;">/</div>

<div>
<input type="number" name="CaseDate_Month__gte" step="any" id="id_CaseDate_Month__gte" class="months">
</div>

<div style="width:6px;height:22px;color: #CCDDEB;font-size:19px;position:absolute;top:58px;left:93px;">/</div>
<div>
<input type="number" name="CaseDate_Year__gte" step="any" id="id_CaseDate_Year__gte" class="years">
</div>

</div>
<div>
<label class="quti" style="position:absolute;top:105px;right:16px;font-size:12px;color: #92A6B6;">تا تاریخ</label>

<div>
<input type="number" name="CaseDate_Day__lte" step="any" id="id_CaseDate_Day__lte" class="enddays">
</div>
<div style="width:6px;height:22px;color: #CCDDEB;font-size:19px;position:absolute;top:133px;left:210px;">/</div>
<div>
<input type="number" name="CaseDate_Month__lte" step="any" id="id_CaseDate_Month__lte" class="endmonths">
</div>
<div style="width:6px;height:22px;color: #CCDDEB;font-size:19px;position:absolute;top:133px;left:93px;">/</div>
<div>
<input type="number" name="CaseDate_Year__lte" step="any" id="id_CaseDate_Year__lte" class="endyears">
</div>
</div>
</div>
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<div>
<label for="incidentTypes" class="quti" style="position:absolute;top:181px;right:16px;font-size:12px;color: #92A6B6;">نوع پرونده</label>

<select name="CaseType" id="id_CaseType" style="top:202px;right:16px;font-family:IRANSans;" class="vorudi">
{% for obj in filter.form.fields.CaseType.choices %}
<option value="{{ obj.0 }}"> {{ obj.1 }} </option>
{% endfor %}
</select>
</div>


<div>
<label for="cities" class="quti" style="top:258px;right:16px;font-size:12px;color: #92A6B6;position:absolute;">واحد پرونده</label>
<select name="CaseType" id="id_CaseType" style="top:279px;right:16px;font-family:IRANSans;" class="vorudi">
{% for obj in filter.form.fields.CaseUnit.choices %}
<option value="{{ obj.0 }}"> {{ obj.1 }} </option>
{% endfor %}
</select>
</div>

<div>
<label for="fraudTypes" class="quti" style="top:335px;right:16px;font-size:12px;color: #92A6B6;position:absolute;">واحد صدور</label>
<select name="CaseType" id="id_CaseType" style="top:356px;right:16px;font-family:IRANSans;" class="vorudi">
{% for obj in filter.form.fields.IssueUnit.choices %}
<option value="{{ obj.0 }}"> {{ obj.1 }} </option>
{% endfor %}
</select>
</div>
<div style="position:absolute;top:410px;width:293px;right:16px;">
<p style="text-align: right;font-size:12px;letter-spacing: 0;color: #92A6B6;;margin-bottom:0;">مشکوک بودن</p>
<input type="range" name="weight" style="width:88%;" id="range_weight" value="5" min="1" max="100" oninput="range_weight_disp.value=range_weight.value">
<output id="range_weight_disp"></output>

</div>

<button type="submit" style="position:absolute;top:458px;margin-top:24px;width: 325px;height: 48px;background: #00559A 0% 0% no-repeat padding-box;
border-radius: 0px 0px 8px 8px;border:none;font-size:18px;color:white;font-family:IRANSans;" onclick="closeForm()">اعمال فیلتر</button>
</form>
</div>
.
.
.

<div id="tableContainer" class="tableContainer">
<table border="0" cellpadding="0" cellspacing="0" width="100%" class="scrollTable" style="font-family:IRANSans;">
<thead class="fixedHeader">
<tr>
<th width="90px" style="border-radius: 8px 0px 0px 0px;"> مشکوک بودن</th>
<th width="90px">نوع پرونده</th>
<th width="90px">واحد پرونده</th>
<th width="90px">واحد صدور</th>
<th width="90px">بیمه گزار</th>
<th width="90px">تاریخ پرونده</th>
<th width="90px" style="border-radius: 0px 8px 0px 0px;">کد پرونده</th>
</tr>
</thead>
<tbody class="scrollContent" style="font-family:IRANSans;">
{% for r in filter.qs %}
<tr style="font-family:IRANSans;">
<td width="200px" style="font-family:IRANSans;">{{r.FraudulantRate}}</td>
<td width="200px" style="font-family:IRANSans;">{{r.CaseType}}</td>
<td width="200px" style="font-family:IRANSans;">{{r.CaseUnit}}</td>
<td width="200px" style="font-family:IRANSans;">{{r.IssueUnit}}</td>
<td width="200px" style="font-family:IRANSans;">{{r.FinalInsurerName}}</td>
<td width="200px" style="font-family:IRANSans;">{{r.CaseDate}}</td>
<td width="200px"><a style="font-family:IRANSans;font-size:14px;" href="{% url 'caria_CaseDetail' r.id %}">{{r.CaseCode}}</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
.
.
.

数据库记录大约为 2000 条,加载大约需要 20 到 30 秒。如何改善性能问题?

任何建议将不胜感激...

首先,调查页面加载缓慢的原因。django-debug-toolbar 对此很有用。

您可能会看到您对每行的外键都有查询(n+1 查询问题(。

您可以使用select_related选择相关对象以防止这些额外的查询。

case_list = models.Fact_CarCase.objects.all().select_related('CaseDate', 'CaseType')

这将停止对CaseDateCaseType的额外查询,但请查看模板或调试工具栏中的 SQL 查询,以查看必须将哪些其他外键添加到列表中。

相关内容

  • 没有找到相关文章

最新更新