Answer the question
In order to leave comments, you need to log in
How to link three tables in django admin?
There are three models:
class Place(models.Model):
place_id = models.AutoField(primary_key=True)
place = models.CharField(max_length=10, null=False)
class Host(models.Model):
host_id = models.AutoField(primary_key=True)
place = models.ForeignKey('Place', on_delete=models.PROTECT)
host = models.CharField(max_length=30, blank=False, null=False)
class Device(models.Model):
device_id = models.AutoField(primary_key=True)
host = models.ForeignKey('Host', on_delete=models.PROTECT)
model = models.ForeignKey('Model', on_delete=models.PROTECT)
ip = models.GenericIPAddressField(null=True)
name = models.CharField(max_length=30, blank=False, null=False)
class DeviceAdmin(admin.ModelAdmin):
list_display=('host', 'name', 'model', 'ip', 'slots')
list_filter = (
'host__place',
('host', admin.RelatedOnlyFieldListFilter),
('model', admin.RelatedOnlyFieldListFilter),
)
def host_place(self, instance):
return instance.host.place
host_location.short_description = 'Place'
host_location.admin_order_field = 'host__place'
admin.site.register(Device, DeviceAdmin)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question