开发者

In django, how can I retrieve a value from db into a custom field template?

I am using a custom class on my model to provide image uploading, through an app 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 filebrowser's custom field "FileBrowserField", which adds a link to a separate upload page (http://site/admin/filebrowser/browse/?ot=desc&o=date). What I'd like to do is to tweak the custom form's template to add a "dir" parameter, like so: (http://site/开发者_如何转开发admin/filebrowser/browse/?ot=desc&o=date&dir=book1). book1, in this case, would be retrieved from the "name" CharField of this Book.

I know that the template that I want to modify is rendered by filebrowser's fields.py, and there is a variable that sets the "dir" parameter, but I don't know how to fetch the string value from my own model to fields.py so I can set this variable. Does anyone have any suggestions?


Found a solution elsewhere, so I thought I'd share it:

# 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜