Django: How to edit a gallery that is attached to a project model?
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 im开发者_JS百科g-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 :)
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)
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..
精彩评论