我想在Django Admin list_display中显示JSON-Field中的特定值。
models.py:
class Foo(models.Model):
client = models.ForeignKey(Client, verbose_name="Kunde")
description = models.CharField(verbose_name=u'Titel', max_length=100)
description_file = models.FileField(verbose_name="Vorschau", upload_to='postings', blank=True, help_text=u'Vorschau/Endfassung')
accepted_by_client = models.BooleanField(verbose_name="abgenommen", help_text=u"")
needs_correction = models.BooleanField(verbose_name=u"Korrektur nötig", help_text=u"")
active = models.BooleanField(verbose_name="Publiziert", help_text=u"")
ctime = models.DateTimeField(verbose_name="Angelegt am", blank=True, null=True)
valid_until = models.DateField(verbose_name=u"gültig bis", blank=True, null=True, default = lambda: datetime.datetime.now()+datetime.timedelta(days=28) )
valid_from = models.DateField(verbose_name=u"gültig ab", blank=True, default=datetime.datetime.now )
link = models.CharField(verbose_name="Formular", max_length=255, blank=True, help_text="generierter Link zum Formular")
admin_notes = models.TextField(u'Notizen für Mitarbeiter', blank=True, help_text=u"")
client_notes = models.TextField(verbose_name=u"Notizen für Kunde", blank=True, help_text=u"")
context = JsonField()
def __unicode__(self):
return self.client.company_name + ' : ' + str(self.id)
admin.py:
class FooAdmin(admin.ModelAdmin):
#list view
def apis(self, object):
data = Foo.context['bar']
return data
list_display = (
'id',
'client',
'description',
'active', 'ctime',
'valid_until',
'valid_from',
'clickable_preview_link',
'clickable_editor_link',
'apis'
)
admin.site.register(Foo, FooAdmin)
我的问题是,我无法从JSON-Field“上下文”访问单个值。我只收到一个空栏。
我该怎么做?
提前致谢!
答案 0 :(得分:2)
class FooAdmin(admin.ModelAdmin):
#list view
def apis(self, object):
data = Foo.context['bar']
return data
list_display = (
'id',
'client',
'description',
'active', 'ctime',
'valid_until',
'valid_from',
'clickable_preview_link',
'clickable_editor_link',
'apis',
'json_data'
)
def json_data(self, obj):
for key, value in obj.context:
return "{0}: {1}".format(key, value)
return ''