1π
β
I finally found a solution that required me to modify my Folder fields so that doesnβt really answer my initial question, but it solved my problem so I put it here in case that can help someone else.
So basically, I changed my parent_folder field from a FilePathField to a Folder ForeignKey, which can be left blank in case I want to put the folder in the root folder :
class Folder(models.Model):
folder_name = models.CharField(max_length=200)
parent_folder = models.ForeignKey('self', on_delete=models.CASCADE, blank=True, null=True)
Now that I have my folders under the form of a ForeignKey, I can modify the queryset and exclude the folder itself from the list of folders availables by overriding the render_change_form method, like so :
class FolderAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['folder_name']}),
('Parent Folder', {'fields': ['parent_folder']}),
]
list_display = ('folder_name', '__str__')
def render_change_form(self, request, context, *args, **kwargs):
context['adminform'].form.fields['parent_folder'].queryset = Folder.objects.exclude(id=context['object_id'])
return super(FolderAdmin, self).render_change_form(request, context, *args, **kwargs)
π€Balizok
Source:stackexchange.com