开发者

Can you access a Django Model "property" from it's ModelForm?

I have a Django model class with a non-model-field property, ex:

def _get(self):
    return "something"
description = property(_get)

I'm using the mod开发者_C百科el class with in a ModelForm / ModelFormset. Is there any way to access the property from the form / formset? If not, what's best practice for including extra "display" data in a django formset?


If your ModelForm was initialized with an instance of a Model, then you can access it through the instance attribute. From the ModelForm docs:

Also, a model form instance bound to a model object will contain a self.instance attribute that gives model form methods access to that specific model instance.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜