Django FSM - get_available_FIELD_transitions

625 Views Asked by At

Using django_fsm I need to get the available transitions a list. When using the following code I do get a <generator object get_available_FIELD_transitions at 0x10b9ba660>

obj = MyModel.objects.get(pk=object_id)
transitions = obj.get_available_status_transitions()
print(transitions)

Instead I would like to get a list of transitions like ['PENDING', 'CLOSED']

2

There are 2 best solutions below

0
Tom Carrick On

The generator has everything you need, it just needs iterating. To get what you want, you can just convert it to a list:

transitions = list(obj.get_available_status_transitions())

You might want to read up on generators in Python, they're very useful.

0
Allan Maina On

Generators are iterable python objects. See Generators

This will print each item

    transitions = list(obj.get_available_state_transitions())
    print(transitions)

I found this from the test cases of django-fsm Django-fsm TestCase