Django 管理站点:需要帮助
Django admin site : Help needed
我正在尝试自定义 djando admin.py.I 想在 class ContributionAdmin
中注册一个模型 Contribution
。在管理界面中,我需要打印一个来自另一个模型 Subscriber
的字段 application_number
编号。与 models.How 有外键关系才能做到这一点?
admin.py
class ContributionAdmin(admin.ModelAdmin):
list_display = ('id','nlcc','subscriber','date','receipt_number','amount', 'status')
list_filter = ('nlcc',)
search_fields = ('receipt_number',)
exclude = ('subscriber',)
admin.site.register(models.Contribution, ContributionAdmin)
models.py:
class Subscriber(models.Model):
nlcc = models.ForeignKey(NLCC)
name = models.CharField(max_length=100,blank=True,null=True)
type = models.CharField(max_length=50, choices=SUBSCRIBER_TYPE)
application_date = models.DateField(null=True)
application_number = models.IntegerField()
staff_agent = models.ForeignKey(StaffAgent, null=True, on_delete=models.SET_NULL)
class Contribution(models.Model):
nlcc = models.ForeignKey(NLCC)
subscriber = models.ForeignKey(Subscriber)
staff_agent = models.ForeignKey(StaffAgent, null=True, on_delete=models.SET_NULL)
date = models.DateField(null=True)
initial = models.BooleanField(default=False)
govt = models.BooleanField(default=False)
receipt_number = models.BigIntegerField()
amount = models.DecimalField(max_digits=10, decimal_places=2)
status = models.CharField(max_length=50, choices=CONTRIBUTION_STATUS)
status_date = models.DateField(null=True)
deleted = models.BooleanField(default=False)
package = models.ForeignKey('CourierPackage', null=True, on_delete=models.SET_NULL, related_name='contribution')
否则有什么方法可以 return Subscriber
名称而不是对象?
创建一个将 return 所需字段的方法并将此方法的名称添加到 list_display
:
class ContributionAdmin(admin.ModelAdmin):
list_display = ('id','nlcc','subscriber','application_number',
'date','receipt_number','amount', 'status')
def application_number(self, obj):
return obj.subscriber.application_number
application_number.short_description = "Subscriber's Application Number"
更新:如果您想在 subscriber
字段的 select 框中查看 name
和 application_number
那么最简单的解决方案就是定义Subscriber
模型的__unicode__
方法:
class Subscriber(models.Model):
...
def __unicode__(self):
return u'%s: %s' % (self.name, self.application_number)
更新 2:如果订阅人数太长,那么您可以使用 raw_id_fields
属性:
class ContributionAdmin(admin.ModelAdmin):
raw_id_fields = ('subscriber', )
更新 3:要将此字段设置为只读,请使用 readonly_fields
:
class ContributionAdmin(admin.ModelAdmin):
readonly_fields = ('subscriber', )
我正在尝试自定义 djando admin.py.I 想在 class ContributionAdmin
中注册一个模型 Contribution
。在管理界面中,我需要打印一个来自另一个模型 Subscriber
的字段 application_number
编号。与 models.How 有外键关系才能做到这一点?
admin.py
class ContributionAdmin(admin.ModelAdmin):
list_display = ('id','nlcc','subscriber','date','receipt_number','amount', 'status')
list_filter = ('nlcc',)
search_fields = ('receipt_number',)
exclude = ('subscriber',)
admin.site.register(models.Contribution, ContributionAdmin)
models.py:
class Subscriber(models.Model):
nlcc = models.ForeignKey(NLCC)
name = models.CharField(max_length=100,blank=True,null=True)
type = models.CharField(max_length=50, choices=SUBSCRIBER_TYPE)
application_date = models.DateField(null=True)
application_number = models.IntegerField()
staff_agent = models.ForeignKey(StaffAgent, null=True, on_delete=models.SET_NULL)
class Contribution(models.Model):
nlcc = models.ForeignKey(NLCC)
subscriber = models.ForeignKey(Subscriber)
staff_agent = models.ForeignKey(StaffAgent, null=True, on_delete=models.SET_NULL)
date = models.DateField(null=True)
initial = models.BooleanField(default=False)
govt = models.BooleanField(default=False)
receipt_number = models.BigIntegerField()
amount = models.DecimalField(max_digits=10, decimal_places=2)
status = models.CharField(max_length=50, choices=CONTRIBUTION_STATUS)
status_date = models.DateField(null=True)
deleted = models.BooleanField(default=False)
package = models.ForeignKey('CourierPackage', null=True, on_delete=models.SET_NULL, related_name='contribution')
否则有什么方法可以 return Subscriber
名称而不是对象?
创建一个将 return 所需字段的方法并将此方法的名称添加到 list_display
:
class ContributionAdmin(admin.ModelAdmin):
list_display = ('id','nlcc','subscriber','application_number',
'date','receipt_number','amount', 'status')
def application_number(self, obj):
return obj.subscriber.application_number
application_number.short_description = "Subscriber's Application Number"
更新:如果您想在 subscriber
字段的 select 框中查看 name
和 application_number
那么最简单的解决方案就是定义Subscriber
模型的__unicode__
方法:
class Subscriber(models.Model):
...
def __unicode__(self):
return u'%s: %s' % (self.name, self.application_number)
更新 2:如果订阅人数太长,那么您可以使用 raw_id_fields
属性:
class ContributionAdmin(admin.ModelAdmin):
raw_id_fields = ('subscriber', )
更新 3:要将此字段设置为只读,请使用 readonly_fields
:
class ContributionAdmin(admin.ModelAdmin):
readonly_fields = ('subscriber', )