Graphviz - AttributeError: object has no attribute 'partition'

Graphviz - AttributeError: object has no attribute 'partition'

我正在使用 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')

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

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