Django:如何编辑附加到项目模型的图库?

发布于 2024-10-13 04:08:08 字数 425 浏览 4 评论 0原文

我有一个“项目”模型。每个项目都有一个“画廊”,每个画廊都有“照片”。

class Project:
 gallery = ForeignKey(Gallery)

class Gallery:
 photos = ManyToManyField(Photo)

class Photo:
 image = ImageField(...)

我想让我的用户在同一页面上编辑图库和项目。您能告诉我需要哪些组件才能实现这一目标吗?就像我应该使用哪种类型的表单以及在处理包含上传的图像等的表单时使用什么技术?

需要考虑的是,我想显示用户正在使用 html img 标签和文件标签编辑的照片,以便他替换照片。我不想要 django 的默认 m2m-widget,它只是一个多选列表。

你能帮我解决这个问题吗,因为我根本做不到。被困在这里三天了:)

I have a 'project' model. Each project has a 'gallery' and each gallery has 'photos'.

class Project:
 gallery = ForeignKey(Gallery)

class Gallery:
 photos = ManyToManyField(Photo)

class Photo:
 image = ImageField(...)

I want to let my users edit the gallery and the project on the same page. Could you tell me what components I need to make this happen? Like which type of form I should use and what technique to use when I process the form with the uploaded images and all?

What to take into account is that I want to show the photos the user is editing with the html img-tag as well as file-tag to let him replace the photo. I don't want django's default m2m-widget which is just a multiselect-list.

Could you help me figure this out, because I simply can't. Been stuck here for three days :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

涫野音 2024-10-20 04:08:08

您可以使用 Project 作为 admin.TabularInline 来修改 Gallery 管理表单。

像这样:

admin.py

# -*- encoding: utf-8 -*-
from models import Project, Gallery, Photo
from django.contrib import admin

class ProjectInline(admin.TabularInline)
    model = Project

class GalleryAdmin(admin.ModelAdmin):
    inlines = [ProjectInline]

admin.site.register(Gallery, GalleryAdmin)

You can modify your Gallery Admin form using Project as a admin.TabularInline.

Like this:

admin.py

# -*- encoding: utf-8 -*-
from models import Project, Gallery, Photo
from django.contrib import admin

class ProjectInline(admin.TabularInline)
    model = Project

class GalleryAdmin(admin.ModelAdmin):
    inlines = [ProjectInline]

admin.site.register(Gallery, GalleryAdmin)
下壹個目標 2024-10-20 04:08:08

我不想使用内置的管理模块。但我使用了 django 的表单集工厂。它会让我向表单集提供查询集(即图库中的照片)。然后我必须提供一个小的定制模型表单集类,然后我认为我几乎必须手动处理表单才能将其正确链接到画廊等。

I didn't want to use the built in admin module. But I used the formset factory by django. It will let me provide a queryset to the formset (i.e the photos in the gallery). Then I had to provide a small customized model formset class, and then i the view I pretty much had to process the form manually in order to link it correct to the gallery and such..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文