Django管理外键下拉与自定义值

Django管理外键下拉与自定义值

本文介绍了Django管理外键下拉与自定义值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个Django模型:

  class Test(models.Model):
pass

class Page(models.Model):
test = models.ForeignKey(Test)

class问题(model.Model):
page = models.ForeignKey页面)

如果我注册了问题给管理员,我下载了一个所需的页面。现在,我需要修改以显示所需的页面 该页面的相应的测试 Page1 Page2 Page3 。我想看到: Page1 - Test1 Page2 - Test1 Page3 - Test1

解决方案

2选项。



1:



创建一个新的字段,复制 forms.ModelChoiceField 并覆盖 label_from_instance

 #从源
class PageModelChoiceField(forms.ModelChoiceField():
def label_from_instance(self,obj):

此方法用于将对象转换为字符串;它用于
生成此对象提供的选项的标签子类
可以覆盖此方法以自定义选项的显示

#然后返回要显示的内容
returnPage {0} - Test {1}。format(obj.pk,obj.test.pk)

这将只会更改该特定下拉字段的文本处理列表中每个项目的测试对象,您可能需要确保 queryset 传递给 PageModelChoiceField select_related('test'),否则它将为列表中的每个项目创建一个DB。



我没有测试这个确切的代码,但逻辑就在那里。稍后尝试,可以

  class QuestionForm(forms.ModelForm):

page = PageModelChoiceField(
queryset = Page.objects.select_related('test')。all()


class Meta:
model = Page


class QuestionAdmin(ModelAdmin):
class Meta:
model:Question
form:QuestionForm
pre>




选项B。



更改 unicode ()表示页面

 类页面(model.Model):
test = models.ForeignKey(Test)

def __unicode __(self):
返回页面{0} - 测试{1}格式(obj.pk,obj.test.pk)

这将改变如何页面显示在打印页面对象的位置, print(page_object) {{page_object}}






我个人偏爱选项1


I have 3 Django models:

class Test(models.Model):
    pass

class Page(models.Model):
    test = models.ForeignKey(Test)

class Question(model.Model):
    page = models.ForeignKey(Page)

If I register the Question model to the admin, I get a dropdown with the desired Page. Now, what do I have to modify to display the desired Page plus that page's corresponding Test?

Say, if I have three pages created, the dropdown will contain these values: Page1, Page2, Page3. I would like to see: Page1 - Test1, Page2 - Test1, Page3 - Test1

解决方案

2 Options.

Option 1:

Create a new field, copy forms.ModelChoiceField and override label_from_instance.

# From the source
class PageModelChoiceField(forms.ModelChoiceField():
    def label_from_instance(self, obj):
        """
        This method is used to convert objects into strings; it's used to
        generate the labels for the choices presented by this object. Subclasses
        can override this method to customize the display of the choices.
        """
        # Then return what you'd like to display
        return "Page{0} - Test{1}".format(obj.pk, obj.test.pk)

This will only change the text for that particular dropdown field. As you are accessing the Test object for each item in the list, you may want to ensure the queryset you pass to the PageModelChoiceField has select_related('test'), otherwise it will make a DB hit for each item on the list.

I've not tested this exact code but the logic is there. Will try it later when I can

class QuestionForm(forms.ModelForm):

    page = PageModelChoiceField(
        queryset=Page.objects.select_related('test').all()
    )

    class Meta:
        model = Page


class QuestionAdmin(ModelAdmin):
    class Meta:
        model: Question
        form: QuestionForm


Option B.

Change the unicode() representation of Page.

class Page(models.Model):
    test = models.ForeignKey(Test)

    def __unicode__(self):
        return "Page{0} - Test{1}".format(obj.pk, obj.test.pk)

This will change how Pages are displayed everywhere you print a page object, print(page_object), {{ page_object }}.


Personally I prefer Option 1

这篇关于Django管理外键下拉与自定义值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 18:53