简体   繁体   中英

Django admin foreign key field cause performance issue

I have a Question model and a Solution model.

class Solution(models.Model):
    user = models.ForeignKey('exam.Users', on_delete=models.CASCADE)
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    solution = RichTextField()

In Django admin, I display a Question_text in Solution section.

@admin.register(Solution)
class SolutionAdmin(admin.ModelAdmin):
    def get_ori_q(self, obj):
        se = obj.question
        return se.question

For this Question_text field, see the picture below.

在此处输入图片说明

But I don't want to edit it, or make it selectable, for it cost a lot of time to display massive item if I click it.

How can I make it read-only and display only one item, instead of displaying all items in Question table?

This should do the trick

@admin.register(Solution)
class SolutionAdmin(admin.ModelAdmin):
    readonly_fields = ('question',)
    def get_ori_q(self, obj):
        se = obj.question
        return se.question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM