E
E
EvgenToHelp2022-01-25 23:47:41
Django
EvgenToHelp, 2022-01-25 23:47:41

Django how to get data from related tables?

There are two models

class Building(models.Model):
    name=models.CharField(max_length=32)

class Room(models.Model):
    number=models.CharField(max_length=8)
    building=models.ForeignKey(Building,on_delete=models.PROTECT)


I want to display in the admin panel
in the second table, it turns out to get data
class RoomEdit(admin.ModelAdmin):
    list_display = ('number' 'showBuilding')


    def showBuilding(self, obj):
        return "\n".join([a.name for a in obj.building.all()])


But I can't figure out how to do the same for the Building model itself

class BuildingEdit(admin.ModelAdmin):
    list_display = ('name' ..........)

    def showBuilding(self, obj):
        ........


admin.site.register(Building, showBuilding)
admin.site.register(Room, RoomEdit)

In the end, that when adding a Building, it was possible to immediately add a Room to it,
or am I stupid and there should be a ManyToMany connection here?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2022-01-26
@deliro

https://docs.djangoproject.com/en/4.0/ref/contrib/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question