Graphviz - AttributeError:对象没有属性'partition'

时间:2017-01-25 19:56:21

标签: python django graphviz

我正在使用graphviz和我的Django模型为每个自动机从以下python脚本中呈现图形:

alphabets = automata.alphabet_set.all()
states = automata.states_set.all()
transitions = automata.transition_set.all()
dot = gv.Graph()
for state in states:
    dot.node(state.state, state.state)
for transition in transitions:
    dot.edge(transition.current_state, transition.next_state, transition.input)
dot.render( automata.id + '.gv', view=True)

这些是我的模特:

class Automata(models.Model):
    pass

class Alphabet(models.Model):
    alphabet = models.CharField()
    automata = models.ForeignKey(Automata, on_delete = models.CASCADE)

class States(models.Model):
    state = models.CharField()
    automata = models.ForeignKey(Automata, on_delete = models.CASCADE)

class Transition(models.Model):
    current_state = models.ForeignKey(States, on_delete = models.CASCADE, related_name = 'current')
    input = models.ForeignKey(Alphabet, on_delete = models.CASCADE)
    next_state = models.ForeignKey(States, on_delete = models.CASCADE, related_name = 'next')
    automata = models.ForeignKey(Automata, on_delete = models.CASCADE)

但每次我尝试执行我的脚本时都会收到以下错误:

Traceback (most recent call last):
  File "make_graph.py", line 36, in <module>
    dot.edge(transition.current_state, transition.next_state, transition.input)
  File "/home/nids/automata/auto/lib/python3.5/site-packages/graphviz/dot.py", line 116, in edge
    tail_name = self.quote_edge(tail_name)
  File "/home/nids/automata/auto/lib/python3.5/site-packages/graphviz/lang.py", line 63, in quote_edge
    node, _, rest = identifier.partition(':')
AttributeError: 'States' object has no attribute 'partition'

如果我只是这样做,我知道我没有错误:dot.edge('A', 'B', 'edge label')

1 个答案:

答案 0 :(得分:2)

graphvis代码期望将字符串传递给它,而不是States对象(您的模型)。

您可以在quote_edge函数的source code中看到这一点。