我正在优化django项目后端,但是在包含ForeignKey

时间:2020-08-06 15:47:08

标签: django optimization django-admin

世界各地的django大师 我是韩国开发人员,三个月前开始了django。

现在我只是公司的奴隶。 无论如何,我在优化django admin项目时遇到问题,但是没有人遇到过同样的问题。

这是我的模型“项目”,“答案”,“请求”。

# ------------------------------------------------------------------
# Model   : Project
# Description : project model
# ------------------------------------------------------------------
class Project(models.Model):

    class Meta:
        verbose_name = '     project'
        verbose_name_plural = '     project'

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

# ------------------------------------------------------------------
# Model   : Request
# Description : Request model
# ------------------------------------------------------------------
class Request(models.Model):
       
    client = models.ForeignKey(Client, on_delete=models.CASCADE, verbose_name='client')
    project = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name='project')
    product = models.ForeignKey(Subclass, on_delete=models.CASCADE, verbose_name='product')
    
    category = models.ManyToManyField(Develop, verbose_name='category')
    
    name = models.CharField('name', max_length=256, blank=True, null=True)
    price = models.CharField('price', max_length=256, blank=True, null=True)
    day = models.CharField('duedate', max_length=256, blank=True, null=True)
    content = RichTextUploadingField('content', null=True)
    file = models.FileField('file', upload_to=request_update_filename, blank=True, null=True)
    created_at = models.DateTimeField('created_at', default=time)
    
    add_meeting = models.BooleanField('add_meeting', default=False, null=True)
    
    examine = models.BooleanField('check examing', default=False, null=True)
    active_save = models.BooleanField('active-save', default=True, null=True)

    class Meta:
        verbose_name = '     request'
        verbose_name_plural = '     requests'

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


class Answer(models.Model):
    client = models.ForeignKey(Client, on_delete=models.CASCADE, verbose_name="client")
    project = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name="project", null=True)
    partner = models.ForeignKey(Partner, on_delete=models.CASCADE, verbose_name="partner")
    
    class Meta:
        verbose_name = '     Answer'
        verbose_name_plural = '     Answer'

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

and this is my admin code

@admin.register(Project)
class ProjectAdmin(admin.ModelAdmin):
    inlines = [RequestInline,AnswerInline]
    list_display = ['request_name','client_email','project_price','project_created','count_answer','count_meeting','answer_status']

    def request_name(self, obj):
        project_id = obj.id
        request_name = Request.objects.get(project = project_id)
        return request_name
        
    def client_email(self, obj):
        project_id = obj.id
        client = Request.objects.get(project=project_id).client
        return client
        
    def client_phone(self, obj):
        project_id = obj.id
        client = Request.objects.get(project=project_id).client
        phone = User.objects.get(username=client).phone
        return phone
    
    def project_price(self, obj):
        project_id = obj.id
        request_price = Request.objects.get(project=project_id).price
        return request_price

    def project_created(self, obj):
        project_id = obj.id
        created_at = Request.objects.get(project=project_id).created_at
        return created_at

    def count_answer(self, obj):
        project_id = obj.id
        answer_qs = Answer.objects.all()
        answer = Answer.objects.filter(project=project_id)
        count = len(answer)
        return count

    def count_meeting(self, obj):
        project_id = obj.id
        answer_yes = Answer.objects.filter(project = project_id, state = 1)
        count = len(answer_yes)
        return count
        
    def answer_status(self, obj):
        project_id = obj.id
        answer = Answer.objects.filter(project = project_id, info_check =1)
        count = len(answer)
        return count

上面没有包括很多因素,但是我要解决的是不要引入相同的查询集(请求,答案)。 项目本身是一个没有属性的结构,仅由外键接收。这种结构使得很难找到解决方案。

我使用了select_related(也是prefetch_related的),但是它不起作用。

如果您可以给我一些建议,我想提一些建议。谢谢。

p.s。这是我第一次在此网站上提问,对于是否有任何不礼貌的行为,我深表歉意。

1 个答案:

答案 0 :(得分:0)

admin.TabularInline将在管理详细信息页面中显示反向外键,这可能会对您有所帮助,但似乎您已经在ProjectAdmin的内联中使用了它。