Answer the question
In order to leave comments, you need to log in
How to display a field from another model to a form?
There are two models:
class MyImage(models.Model):
image = models.ImageField(upload_to='images/hotels', verbose_name='Фото')
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey("content_type", "object_id")
class Hotels(models.Model):
title = models.CharField(max_length=200, verbose_name="Название")
slug = models.SlugField(unique=True, verbose_name="URL")
from django.contrib.contenttypes import generic
class MyImageInline(generic.GenericTabularInline):
model = MyImage
class HotelsAdmin(admin.ModelAdmin):
inlines = [
MyImageInline,
]
Answer the question
In order to leave comments, you need to log in
Add an image field to the hotels model via a ForeignKey. Then there will be an image field in the form of a hotel that refers to a single image.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question