1

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?

Leap
  • 158
  • 11
  • Have you read the guide below? – Fomalhaut Aug 23 '18 at 15:29
  • I think this section is what you need: https://docs.djangoproject.com/en/2.1/howto/custom-model-fields/#writing-a-field-subclass – Fomalhaut Aug 23 '18 at 15:32
  • still doesn't explain example.hand = new_hand – Leap Aug 23 '18 at 16:02
  • You can't create a field class easily without inheritance from `models.Field` or its subclasses, because field class has to have implemented special methods that make it work correctly with Django models. – Fomalhaut Aug 23 '18 at 16:09

0 Answers0