Reputation: 133
Here is my code. models.py
from django.db import models
class Photo(models.Model):
file = models.ImageField(upload_to='media/images')
class Album(models.Model):
title = models.CharField(max_length=100)
photos = models.ManyToManyField(Photo,related_name="photos",blank=True)
admin.py
from django.contrib import admin
from .models import Photo, Album
# Register your models here.
class AlbumAdmin(admin.ModelAdmin):
list_display = ("title","published")
filter_horizontal = ("photos",)
def formfield_for_manytomany(self, db_field, request, **kwargs):
if db_field.name == "photos":
kwargs["queryset"] = Photo.objects.order_by("-id")
return super(AlbumAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)
admin.site.register(Photo)
admin.site.register(Album, AlbumAdmin)
views.py
from django.shortcuts import render
from django.conf import settings
from .models import Photo, Album
# Create your views here.
def album(request, album_title):
album = Album.objects.get(title=album_title)
context = {
"album":album,
}
return render(request, "photos/album.html", context)
I am trying to achieve a page that shows the photos in each album and has a drag-and-drop function that allows a manual re-ordering of the photos in each album. Then I wish to save the order of the album. The only problem is, I have no clue to how to do this.
Upvotes: 1
Views: 1171
Reputation: 1552
It's not a trivial task, so you can't just expect a one line answer, but I guess it's a good use case to have a look at some packages that try to solve this.
Here is a good overview of all packages that try to solve ordering, maybe one of them fits you well: https://djangopackages.org/grids/g/model-ordering/
Upvotes: 2