django自定义模型字段

时间:2018-08-23 15:25:15

标签: django django-models django-custom-field

我正在尝试按照本指南创建自己的自定义模型字段 https://docs.djangoproject.com/en/2.1/howto/custom-model-fields/

class Hand:
    """A hand of cards (bridge style)"""

    def __init__(self, north, east, south, west):
        # Input parameters are lists of cards ('Ah', '9s', etc.)
        self.north = north
        self.east = east
        self.south = south
        self.west = west

    # ... (other possibly useful methods omitted) ...

下一步是(我们假设模型上的hand属性是Hand的实例)

example = MyModel.objects.get(pk=1)
print(example.hand.north)

new_hand = Hand(north, east, south, west)
example.hand = new_hand
example.save()

“我们假设模型上的hand属性是Hand的实例”

有人可以提供例子吗?

0 个答案:

没有答案