I have the following classes in my models.py files
class Node(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
class Edge(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
source_node = models.ForeignKey(Node, on_delete=models.CASCADE, related_name='edge_sources', related_query_name='edge_source')
target_node = models.ForeignKey(Node, on_delete=models.CASCADE, related_name='edge_targets', related_query_name='edge_target')
When I add an edge using Django’s admin interface I get the following error:
TypeError at /admin/edges/edge/add/
__str__ returned non-string (type Node)
Request Method: POST
Request URL: http://localhost:8000/admin/edges/edge/add/
Django Version: 4.0.4
Exception Type: TypeError
Exception Value: __str__ returned non-string (type Node)
Exception Location: /Users/me/Documents/project/venv/lib/python3.10/site-packages/django/contrib/admin/options.py, line 921, in log_addition
Python Executable: /Users/me/Documents/project/venv/bin/python3
Python Version: 3.10.4
It’s only the Edge class that’s causing trouble. For instance, I have a class Label:
class Label(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
node = models.ForeignKey(Node, on_delete=models.CASCADE, related_name='labels', related_query_name='label')
name = models.CharField(max_length=64)
I can add labels using Django’s admin, without getting errors. I wonder whether referencing class Node twice from the class Edge is causing the problem. If so, is there a solution.
Kind regards,
Johanna