django custom model fields

1.1k Views Asked by At

Im trying to create my own custom model field following this guide 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) ...

next step is(we assume the hand attribute on the model is an instance of Hand)

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

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

"we assume the hand attribute on the model is an instance of Hand"

Can someone provide example please?

0

There are 0 best solutions below