每个模型多个图像 [英] Multiple images per Model
问题描述
所以说,我有什么选择?
-
有没有我不知道的ImageList字段?
-
有没有像
django.contrib.comments
这样的应用程序,为我做的工作? -
如果我必须自己写,我该怎么做,使管理层体面?我想象的是像ImageField提供的东西,比较多,有些拖动重新排序。但是我写的是一个完整的写作管理页面=(
-
变量列表(也称为多对一关系)通常通过为许多单独的模型(在该模型中)使用ForeignKey到一来处理。
li>
管理员网站不能被用户证明,只能由受信任的用户使用。鉴于此,使您的管理员网站体面的方式是为您的属性定义一个ModelAdmin,然后内联照片(内联文档)。
所以,给你一些快速草稿,一切都会像这样:
#models.py
class Property(models.Model):
address = models.TextField()
...
class PropertyImage(models.Model):
property = models。 ForeignKey(Property,related_name ='images')
image = models.ImageField()
和:
#admin.py
class PropertyImageInline(admin.TabularInline):
model = PropertyImage
extra = 3
class PropertyAdmin(admin.ModelAdmin):
inlines = [PropertyImageInline,]
在ForeignKey上使用related_name参数的原因是,您的查询将更易读,例如在这种情况下,您可以在您的视图中执行以下操作:
property = Property.objects.get(pk = 1)
image_list = property.images.all()
编辑:忘记提及,你可以使用Simon Willison的代码片段,在管理员中实施拖放顺序使用jQuery UI进行拖放可订购内联
I'm writing a simple real-estate listing app in Django. Each property needs to have a variable number of images. Images need to have an editable order. And I need to make the admin user-proof.
So that said, what are my options?
Is there a ImageList field that I don't know about?
Is there an app like
django.contrib.comments
that does the job for me?If I have to write it myself, how would I go about making the admin-side decent? I'm imagining something a lot slicker than what ImageField provides, with some drag'n'drop for re-ordering. But I'm a complete clutz at writing admin pages =(
Variable lists, also known as a many-to-one relationship, are usually handled by making a separate model for the many and, in that model, using a ForeignKey to the "one".
There isn't an app like this in django.contrib, but there are several external projects you can use, e.g. django-photologue which even has some support for viewing the images in the admin.
The admin site can't be made "user proof", it should only be used by trusted users. Given this, the way to make your admin site decent would be to define a ModelAdmin for your property and then inline the photos (inline documentation).
So, to give you some quick drafts, everything would look something like this:
# models.py
class Property(models.Model):
address = models.TextField()
...
class PropertyImage(models.Model):
property = models.ForeignKey(Property, related_name='images')
image = models.ImageField()
and:
# admin.py
class PropertyImageInline(admin.TabularInline):
model = PropertyImage
extra = 3
class PropertyAdmin(admin.ModelAdmin):
inlines = [ PropertyImageInline, ]
The reason for using the related_name argument on the ForeignKey is so your queries will be more readable, e.g. in this case you can do something like this in your view:
property = Property.objects.get(pk=1)
image_list = property.images.all()
EDIT: forgot to mention, you can then implement drag-and-drop ordering in the admin using Simon Willison's snippet Orderable inlines using drag and drop with jQuery UI
这篇关于每个模型多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!