Saturday, August 21, 2010

mutually referential models

As a test application to learn Django, I'm trying to model a network of computers,
switches, patch panels and the connections between them. As a start, I have:


class Node(models.Model):
name = models.CharField(max_length=80)
description = models.CharField(max_length=80, null=True)
nodeLeft = models.ForeignKey('self')
nodeRight = models.ForeignKey('self')
# name of wire connecting to left and right nodes
labelLeft = models.CharField(max_length=20, null=True)
labelRight = models.CharField(max_length=20, null=True)

class Computer(models.Model):
name = models.CharField(max_length=80)
description = models.CharField(max_length=80, null=True)
interfaces = models.ManyToManyField(ComputerInterface)

class ComputerInterface(Node):
ipAddr = models.IPAddressField(null=True)
mbitsPerSec = models.IntegerField(null=True)
computer = models.OneToOneField(Computer)

I run into a problem because ComputerInterface is not defined at the time its needed for
the "Computer" class:

File "/home/andy/src/django-test/netlist/nodes/models.py", line 17, in Computer
interfaces = models.ManyToManyField(ComputerInterface)
NameError: name 'ComputerInterface' is not defined

Is there a way I can refer to a class not yet defined?

Thanks,

Andy

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate