Django admin radio-button for one-to-one relationship

465 views Asked by At

I have 2 model classes in my django app: 1: House (name, location, id...) 2: Photo (description, id, house -foreign key-).

In the admin interface, the photo is displayed as inline for the house form, but now, I want the user to be able to choose ONE picture as highlighted for that house. My question is: there's someway to add a radiobutton so the user is only able to choose one picture? Could you help me to achieve this, please? Thanks!

1

There are 1 answers

0
ilvar On

Add OneToOneField for Photo in House model (you will need to reference it as "YOUR_APP.Photo" to avoid circular references), provide ModelAdmin with custom form, and in that form's constructor filter queryset for that field to display only photos for current house.

Different approach is to add highlighted flag in Photo model and ensure on save that only one photo is highlighted for one house.