我有以下代码:
class TelefoneIPInline(admin.StackedInline):
model = MovimentoTelefoneIP
extra = 1
list_filter = ['nome']
class TelefoneIPAdmin(admin.ModelAdmin):
list_display = ('mac', 'modelo', 'fornecedor', )
list_filter = ['cliente__nome']
search_fields = ['mac']
inlines = [TelefoneIPInline]
actions = [export_as_csv_action("Exportar para Excel", fields=['mac','modelo','cliente'])]
我想知道是否有办法使用来自 MovimentoTelefoneIP 的字段,该字段是 TelefoneIP 的相关类,作为 list_display、list_display 或操作中的“字段”中的参数。
这是我的模型:
class TelefoneIP(models.Model):
mac = models.CharField(max_length=12, help_text="Este campo deve ter 12 digitos")
modelo = models.ForeignKey(ModeloTelefoneIP)
cliente = models.ForeignKey(Cliente, verbose_name="Cliente Atual")
fornecedor = models.ForeignKey(Fornecedor)
datacompra = models.DateField(verbose_name="Data de compra", null=True, blank=True)
nfcompra = models.IntegerField(verbose_name="Nota fiscal de compra", blank=True, null=True)
obs = models.TextField(max_length=500, null=True, blank=True)
class MovimentoTelefoneIP(models.Model):
equipamento = models.ForeignKey(TelefoneIP)
cliente = models.ForeignKey(Cliente)
nfvenda = models.IntegerField(verbose_name="Nota fiscal de saída", null=True, blank=True)
datavenda = models.DateField(verbose_name="Data de saída", null=True, blank=True)
谢谢。
尝试了克里斯的回答:
class TelefoneIPAdmin(admin.ModelAdmin):
cliente = MovimentoTelefoneIP.objects.all()
list_display = ('mac', 'modelo','get_clientes')
search_fields = ['mac']
list_filter = ['get_clientes']
def get_clientes(self, obj):
clientes = [m.cliente for m in obj.MovimentoTelefoneIP_set.all()]
return u', '.join(clientes)
get_clientes.short_description = 'Clientes'
inlines = [TelefoneIPInline]
actions = [export_as_csv_action("Exportar para Excel", fields=['mac','modelo','fornecedor'])]