我正在制作一个模型女巫有一个CommaSeparatedIntegerField我存储了一些图像的顺序,我从文件夹FolderField获取。
class Gallery(models.Model)
[…]
folder = FilerFolderField(blank=False)
order = models.CommaSeparatedIntegerField(max_length=300, blank=True)
现在,我正在寻找一种方法,可以在Django管理员中轻松更改此顺序(例如通过拖放)
P.S。我知道FilerFolderField尚未记录,可以删除,并且不推荐使用CommaSeparatedIntegerField。
答案 0 :(得分:0)
正如e4c5的评论中所建议的那样,我大幅改变了我的命令。 我现在有一个带有FilerFolderField的模型(仍未记录)和一个filesm.model.mmage的sortedm2m,其中包含所选文件夹中所有文件的自定义查询集。
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)