'Get field value from inside the model class

I have a Folder class, which creates a folder when instanciated, with the parent_folder field being the location in which I want the folder to be created.

class Folder(models.Model):
    folder_name = models.CharField(max_length=200, unique=True)
    parent_folder = models.FilePathField(path=r'media/', allow_files=False, allow_folders=True, recursive=True)

    
    def __init__(self, *args, **kwargs):
        super(Folder, self).__init__(*args, **kwargs)
        self.initial_filepath = self.filepath()


    def save(self, *args, **kwargs):

        on_creation = self.pk is None

        if on_creation:
            os.mkdir(self.filepath())   
        else:
            os.rename(self.initial_filepath, self.filepath())

        super(Folder, self).save(*args, **kwargs)

        def filepath(self):
            return os.path.join(self.parent_folder, self.folder_name)

When I go to the Django Admin page, there's no issue while creating a folder, however if I go and try to modify it, among the different choices for parent_folder, there's the folder itself, and if I select it, it causes an error because it tries renaming inside itself.

My goal is then to remove the folder from the list of folders in which I can move it. I tried using the match attribute of the FilePathField, but I didn't manage to get the value of folder_name of my instance (to match any folder which doesn't contain the name of the folder I'm moving).

So if you could help me get the value of folder_name of my instance that would be really appreciated !



Solution 1:[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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Balizok