私は次のコードを持っています:
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'])]
TelefoneIPの関連クラスであるMovimentoTelefoneIPのフィールドを、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'])]