Django models and Python properties

后端 未结 1 619
长发绾君心
长发绾君心 2021-02-13 19:27

I\'ve tried to set up a Django model with a python property, like so:

class Post(models.Model):
    _summary = models.TextField(blank=True)
    body = models.Tex         


        
相关标签:
1条回答
  • 2021-02-13 19:59

    Unfortunately, Django models don't play very nice with Python properties. The way it works, the ORM only recognizes the names of field instances in QuerySet filters.

    You won't be able to refer to summary in your filters, instead you'll have to use _summary. This gets messy real quick, for example to refer to this field in a multi-table query, you'd have to use something like

    User.objects.filter(post___summary__contains="some string")
    

    See https://code.djangoproject.com/ticket/3148 for more detail on property support.

    0 讨论(0)
提交回复
热议问题