简体   繁体   English

Django Admin.py自定义表单

[英]Django Admin.py customize form

I've following models:- 我有以下模特:-

from django.db import models

class A(models.Model):
    name = models.CharField(max_length=60)

class B(models.Model):
    row1 = models.CharField(max_length=60)
    a = models.ForeignKey(A)

I've enabled admin module. 我已启用admin模块。 Lets say I've added 3 objects of A with name values as Roy , Jack , Joe . 可以说我添加了3个名称为RoyJackJoeA对象。 So now while adding a new B object from the admin panel, the row a (which is a FK from A) has a drop-down value, with 3 values as A object (all same), which is very difficult to link. 因此,现在在通过管理面板添加新的B对象时,行a (这是A的FK)具有一个下拉值,其中3个值作为A object (都相同),很难链接。 Can I customize these values with the names from A ?? 我可以使用A中的名称来自定义这些值吗?

I know I can create an admin.py file, and create a class such as this:- 我知道我可以创建一个admin.py文件,并创建一个这样的类:-

class A_Admin(admin.ModelAdmin):
    list_display = ('name',)

But this is only for displaying the information in the list, not while selecting the dropdown values. 但这仅用于显示列表中的信息,而不是在选择下拉值时显示。 How do I display the name from A while adding an object of B . 添加对象B时如何显示A的名称。

Modified my class as follow and it worked. 修改了我的班级,使其有效。 :) :)

class A(models.Model):
    name = models.CharField(max_length=60)
    def __unicode__(self):
        return self.name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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