0

I'm working on a model which has an CommaSeparatedIntegerField in which I store the order of some Images which I'm getting from filers FolderField.

class Gallery(models.Model)
    […]
    folder = FilerFolderField(blank=False)
    order = models.CommaSeparatedIntegerField(max_length=300, blank=True)

Now I'm looking for a way to be able to change this order easily (e.g. by drag and drop) in the Django admin

P.S. I know that FilerFolderField is not documented yet and could be removed, and also that CommaSeparatedIntegerField is deprecated.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Nick Ehlers
  • 16
  • 1
  • 3
  • Don't save CSV in a column http://stackoverflow.com/questions/41304945/best-type-of-indexing-when-there-is-like-clause/41305027#41305027 http://stackoverflow.com/questions/41215624/sql-table-with-list-entry-vs-sql-table-with-a-row-for-each-entry/41215681#41215681 – e4c5 Jun 12 '17 at 12:17
  • Also note that this field was (thankfully) deprecated in django 1.9 – e4c5 Jun 12 '17 at 12:17
  • So, depending on this given situation, which practice do you suggest? – Nick Ehlers Jun 12 '17 at 12:24
  • normalizing your database. – e4c5 Jun 12 '17 at 12:24

1 Answers1

0

As suggested in the comments by e4c5, I changed my appoach massivly. I now have a model with a FilerFolderField (still not documented) and a sortedm2m for filer.models.Image with a custom queryset of all files in the selected folder.

models.py:

class Car(models.Model):
    …
    folder = filer.fields.folder.FilerFolderField(blank=False)
    images = SortedManyToManyField(filer.models.Image)
    …

admin.py:

class CarAdminForm(ModelForm):
class Meta:
    model = Car
    fields = '__all__'

def __init__(self, *args, **kwargs):
    super(CarAdminForm, self).__init__(*args, **kwargs)
    choices = []
    for i in self.instance.folder.files.all():
        i.__str__ = thumb_string
        choices.append(i)
    self.fields['images'].queryset = self.instance.folder.files.all()


class CarAdmin(admin.ModelAdmin):
    list_display = ['name']
    list_filter = ['created']

    def __init__(self, model, admin_site):
        super(CarAdmin, self).__init__(model, admin_site)
        self.orig_form = self.form

    def change_view(self, request, object_id, form_url='',     extra_context=None):
        self.form = CarAdminForm
        self.prepopulated_fields = {}
        self.exclude = []
        return super(CarAdmin, self).change_view(request, object_id)

    def add_view(self, request, form_url='', extra_context=None):
        self.form = self.orig_form
        self.prepopulated_fields = {"slug": ("name",)}
        self.exclude = ('images', )
        return super(CarAdmin, self).add_view(request)


class CategoryAdmin(admin.ModelAdmin):
    list_display = ['title', 'description']




admin.site.register(Car, CarAdmin)
admin.site.register(Category, CategoryAdmin)
Nick Ehlers
  • 16
  • 1
  • 3