0

I have a cage I want to add. I have this list with information about this cage, but want to add two more items; site and company. Where I want to add "site" and "company"

In when adding cages it is possible to choose company and site with ForeignKey, as you see in models.py code here:

class Cage(models.Model):
external_id = models.CharField(max_length=200, null=False)
name = models.CharField(max_length=200, null=False)
site = models.ForeignKey(
    Site, null=True,
    on_delete=models.PROTECT
)
company = models.ForeignKey(
    Company, null=True,
    on_delete=models.PROTECT,
)
latitude = models.FloatField(null=True, blank=True)
longitude = models.FloatField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

def __str__(self):
    return self.name

My admin.py code looks like this:

class CageAdmin(admin.ModelAdmin):
list_display = ('id', 'external_id', 'name', 'drone_match', 'latitude', 'longitude')

def drone_match(self, obj):
    drone_cages = DroneCage.objects.filter(cage_id=obj.id)
    link = ''
    if len(drone_cages) > 1:
        link = 'multiple drones'
    elif len(drone_cages) == 1:
        drone = Drone.objects.filter(id=drone_cages[0].drone_id)[0]
        link = format_html('<a href="{}">{}</a>'.format(
            reverse('admin:baleen_drone_change', args=(drone.id, )),
            drone.serial
        ))
    return link
drone_match.short_description = 'Current Drone'

and when I add site and company in list_display like this:

list_display = ('id', 'external_id', 'name', 'site', 'company', 'drone_match', 'latitude', 'longitude')

I get the following error:

Unknown column 'site_id' in 'field list' when listing new list items.

I can't figure out why site and company do not work when latitude and name for example do. In advance, thank you.

Morten
  • 23
  • 3

1 Answers1

0

It's a bit more complicated when referencing foreign keys. Here's a good explanation: https://stackoverflow.com/a/23747842/1536402

Ed Kohler
  • 534
  • 3
  • 9