I have two basic models, Product and Image with the latter assigned to the Product via Foreign Key:
class Image(models.Model):
name = models.CharField(max_length=100)
image = models.ImageField(upload_to=sku_dir)
description = models.CharField(max_length=100, default='', blank=True)
product = models.ForeignKey(product.Product, on_delete=models.CASCADE, related_name='images')
In admin, I am using the Image model inline:
class ImageInline(admin.TabularInline):
model = image.Image
extra = 1
@admin.register(product.Product)
class ProductAdmin(admin.ModelAdmin):
search_fields = ['sku', 'name']
list_display = ['sku', 'name', 'price']
autocomplete_fields = ['genus']
inlines = [ImageInline,]
I would like to add an additional field to Image to control which image is displayed by default - ideally there would be a radio button for each Image displayed inline on the Product admin form and only one of the images could be selected at once.
As I am still learning Django, I sense there is an easy way to do this but I don't know the proper terminology specific to Django to search for an answer (similar to how it took me awhile to discover that "inline" was a term used to display one model's form inside another).
How can I add a radio button to each Image which only allows one Image to be selected in the inline form?