B
B
blackbb2015-11-07 00:35:21
Django
blackbb, 2015-11-07 00:35:21

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")

and
class Hotels(models.Model):
    title = models.CharField(max_length=200, verbose_name="Название")
    slug = models.SlugField(unique=True, verbose_name="URL")

in admin.py:
from django.contrib.contenttypes import generic

class MyImageInline(generic.GenericTabularInline):
    model = MyImage

class HotelsAdmin(admin.ModelAdmin):
    inlines = [
        MyImageInline,
    ]

I display the hotels model in the form, all fields are displayed normally, except for image. The question is how to display the image field so that the user can use it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav Sklyar, 2015-11-07
@VladSkliar

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 question

Ask a Question

731 491 924 answers to any question