Reputation: 73
i have field('image_tag') from Inlinemodel that i want to display in one row of Orderdetail model.
class SampleImagesInline(admin.StackedInline):
fields = ['image_tag']
readonly_fields = ['image_tag']
model = SampleImages
extra = 0
@admin.register(OrderDetail)
class OrderDetailAdmin(admin.ModelAdmin):
inlines = [SampleImagesInline]
by default these are showing vertically. how to display in one row?.
Upvotes: 2
Views: 2990
Reputation: 51988
You can use TabularInline. Try like this:
class SampleImagesInline(admin.TabularInline):
fields = ['image_tag']
readonly_fields = ['image_tag']
model = SampleImages
extra = 0
I think I misunderstood your problem. IMHO, you should not use the InLineAdmin. Instead, try like this:
from django.utils.safestring import mark_safe
...
class OrderDetailAdmin(admin.ModelAdmin):
...
readonly_fields = ['image_tags',]
def image_tags(self, obj):
img_html = ""
for image in obj.image_set.all(): # <-- get related images
img_html += "<img src={}> ".format(image.image.url)
same_line_html = '<div class="tabular inline-related last-related">{}</div>'.format(img_html)
return mark_safe(same_line_html)
image_tags.description = "Images"
Please see here in docs for more information on getting related objects
Upvotes: 3