1

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.

1 Answer 1

2

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/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I used django-admin-sortable2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.