In django, how can I get the value from db to a custom field template?

I use my own class for my model to ensure that images are downloaded through an application called django-filebrowser .

# myapp/models.py
class Book(models.Model):
    name = models.CharField(max_length=30)
    image = FileBrowseField("Image", max_length=200, blank=True, null=True)
    ...

The model uses the custom filebrowser field "FileBrowserField", which adds a link to a separate download page (http: // site / admin / filebrowser / browse /? Ot = desc & o = date). What I would like to do is set up a custom form template to add the "dir" parameter, for example: (http: // site / admin / filebrowser / browse /? Ot = desc & o = date & dir = book1) . book1, in this case, will be extracted from the CharField "name" of this book.

I know that the template I want to change is displayed using filebrowser fields.py, and there is a variable that sets the "dir" parameter, but I don’t know how to get the string value from my own model for fields.py, so I I can set this variable . Anyone have any suggestions?

+3
source share
1 answer

Found a solution elsewhere, so I decided to share with him:

# models.py
class Book(models.Model):
    name = models.CharField(max_length=30)
    image = FileBrowseField("Image", max_length=200, blank=True, null=True)

...

    def __init__(self, *args, **kargs):                                                
        super(Property, self).__init__(*args, **kargs)                             
        self._meta.get_field_by_name("image")[0].directory = self.name
+2
source

Source: https://habr.com/ru/post/1775173/


All Articles