【发布时间】:2020-04-15 21:53:07
【问题描述】:
我有这两个模型
from django.db import models
def get_upload_path(instance, filename):
return '{0}/{1}'.format(instance.imovel.id, filename)
# Create your models here.
class Imovel(models.Model):
nome = models.CharField(max_length=50)
descricao = models.CharField(max_length=800)
area = models.IntegerField()
quartos = models.SmallIntegerField(null=True, blank=True)
banheiros = models.SmallIntegerField()
disponivel_aluguel = models.BooleanField()
disponivel_venda = models.BooleanField()
valor_aluguel = models.DecimalField(max_digits=15, decimal_places=2)
valor_venda = models.DecimalField(max_digits=15, decimal_places=2)
valor_condominio = models.DecimalField(max_digits=15, decimal_places=2)
valor_iptu = models.DecimalField(max_digits=15, decimal_places=2)
capa = models.OneToOneField('ImagemImovel', related_name='capa', on_delete=models.DO_NOTHING, null=True, blank=True)
def __str__(self):
return self.nome
class Meta:
db_table = 'imovel'
class ImagemImovel(models.Model):
imovel = models.ForeignKey(Imovel, related_name='imagens', on_delete=models.CASCADE)
nomeImagem = models.CharField(max_length=20)
imagem = models.ImageField(upload_to=get_upload_path)
def __str__(self):
return self.nomeImagem
class Meta:
db_table = 'imagemImovel'
我有房子,它是图片和一个名为“capa”的字段,用来说明哪一个是主要图片。 问题是当我通过 django admin 添加房子时,保存并返回选择主要的房子,它让我从其他房子中选择图像。如何过滤它以仅显示与该特定房屋相关的图像?
我的 django 管理文件
from django.contrib import admin
from .models import ImagemImovel, Imovel
# Register your models here.
class ImagemImovelAdmin(admin.TabularInline):
model = ImagemImovel
class ImovelAdmin(admin.ModelAdmin):
inlines = (ImagemImovelAdmin, )
admin.site.register(Imovel, ImovelAdmin)
【问题讨论】:
-
我不太明白。在当前设置中,当您在管理面板中单击 Imovel 模型时,您应该能够看到关联的图像。能详细解释一下吗?
-
当我在管理面板中时,由于 admin.py 中的“inlines = (ImagemImovelAdmin, )”,我可以在同一个地方添加房子和房子的图像,我想要的是过滤字段“capa”中的选项
标签: django django-rest-framework django-admin