为什么我的django管理员会导致性能问题?

时间:2018-12-04 19:11:07

标签: python django django-admin django-queryset

我真的需要帮助来了解为什么django管理员会导致性能问题? visvists表大约有1lkh行,我必须加入大约8个不同的模型类(带有外键)才能获得完整的访问数据集。我已经搜索/阅读了很多有关Django管理员优化的信息,虽然也有一些想法,但是我无法在现有项目中实现它。下面是我的admin.py和models.py文件的快照。

admin.py

class VisVisitsAdmin(admin.ModelAdmin):
list_display = ('visit_id','program_name','state_name','district_name','block_name','school_name')
fieldsets = [
    ['General information', {
        'fields': ['school_program','visit_id','user']
    }],
    ['Optional Field', {
        'classes': ['collapse'],
        'fields': [('visit_no', 'is_valid','ac_year')],
    }],
]

def block_name(self, obj):
    if obj.school_program:
        # return obj.village.cluster.block.block_name
        return obj.school_program.school.cluster.block.block_name
block_name.short_description = 'block name'

def district_name(self, obj):
    if obj.school_program:
        # return obj.village.cluster.block.district.district_name
        return obj.school_program.school.cluster.block.district.district_name
district_name.short_description = 'district name'

def state_name(self, obj):
    if obj.school_program:
        # return obj.village.cluster.block.district.state.name_of_state
        return obj.school_program.school.cluster.block.district.state.name_of_state
state_name.short_description = 'state name'

def program_name(self,obj):
    if obj.school_program:
        return obj.school_program.program.program_name

def school_name(self,obj):
    if obj.school_program:
        return obj.school_program.school.school_name

admin.site.register(VisVisits,VisVisitsAdmin)

model.py

    class VisVisits(models.Model):
    visit_id = models.IntegerField(primary_key=True)
    visit_no = models.IntegerField(blank=True, null=True)
    school_program = models.ForeignKey(SchProgramForSchools, models.DO_NOTHING, blank=True, null=True)
    user = models.ForeignKey(UsrUsers, models.DO_NOTHING, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'vis_visits'

    def __str__(self):
        return str(self.visit_id)


class SchPrograms(models.Model):
    program_id = models.IntegerField(primary_key=True)
    program_name = models.CharField(max_length=200, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'sch_programs'

    def __str__(self):
        return self.program_name

class SchPartneredStates(models.Model):
    state_id = models.IntegerField(primary_key=True)
    name_of_state = models.CharField(max_length=100, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'sch_partnered_states'

    def __str__(self):
        return str(self.name_of_state)



class SchDistricts(models.Model):
    district_id = models.IntegerField(primary_key=True)
    state = models.ForeignKey(SchPartneredStates, models.DO_NOTHING, blank=True, null=True)
    district_name = models.CharField(max_length=100, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'sch_districts'

    def __str__(self):
        return str(self.district_name)


class SchBlocks(models.Model):
    block_id = models.IntegerField(primary_key=True)
    district = models.ForeignKey(SchDistricts, models.DO_NOTHING, blank=True, null=True)
    block_name = models.CharField(max_length=100, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'sch_blocks'

    def __str__(self):
        return str(self.block_name)


class SchClusters(models.Model):
    cluster_id = models.IntegerField(primary_key=True)
    block = models.ForeignKey(SchBlocks, models.DO_NOTHING, blank=True, null=True)
    cluster_name = models.CharField(max_length=100, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'sch_clusters'

    def __str__(self):
        return str(self.cluster_name)


class SchVillages(models.Model):
    village_id = models.IntegerField(primary_key=True)
    cluster = models.ForeignKey(SchClusters, models.DO_NOTHING, blank=True, null=True)
    village_name = models.CharField(max_length=100, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'sch_villages'


    def __str__(self):
        return str(self.village_name)


class SchSchools(models.Model):
    school_id = models.IntegerField(primary_key=True)
    school_name = models.CharField(max_length=500, blank=True, null=True)
    cluster = models.ForeignKey(SchClusters, models.DO_NOTHING, blank=True, null=True)
    village = models.ForeignKey(SchVillages, models.DO_NOTHING, blank=True, null=True)


    class Meta:
        managed = False
        db_table = 'sch_schools'

    def __str__(self):
        return str(self.school_name)

class SchProgramForStates(models.Model):
    pfst_id = models.IntegerField(primary_key=True)
    program = models.ForeignKey(SchPrograms, models.DO_NOTHING, blank=True, null=True)
    state = models.ForeignKey(SchPartneredStates, models.DO_NOTHING, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'sch_program_for_states'

0 个答案:

没有答案