我尝试在电路模型类的管理类中过滤外键字段的对象:
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "current_firmware":
kwargs["queryset"] = FirmwareVersion.objects.filter(firmware__circuit_model=self.model)
return super(CircuitAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
我不知道为什么,但current_firmware
过滤后字段的选择列表为空。也许任何人都可以帮助我。我有这些课程:
class FirmwareVersion(models.Model):
firmware = models.ForeignKey(Firmware)
version = models.CharField(max_length=100)
class Firmware(models.Model):
name = models.CharField(max_length=250)
circuit_model = models.ForeignKey(Versiiplates)
class Versiiplates (Model):
version = ForeignKey(Plates)
nomer_oboznachenie = CharField(max_length=8)
class Circuit(models.Model):
circuit_id = models.PositiveIntegerField(blank=True, null=True)
model = models.ForeignKey(Versiiplates)
current_firmware = models.ForeignKey(FirmwareVersion, blank=True, null=True)