Question about models

15 views
Skip to first unread message

Nathan Harmston

unread,
Jan 13, 2007, 12:20:25 PM1/13/07
to django...@googlegroups.com
Hi,

I ve decided to try to use django in a project of mine to display some results. I have an idea of how I m going to implement the database but not how this translate to a django data model. I am trying to use a database to store a graph of Nodes to Nodes with Edges. However there are multiple edge types and multiple node types, I m trying to make this as generic as possible.

e.g Node1_(n typeA) --Edge1_(e typeA) ---> Node2_(n typeA)
Node1_(n typeA) --Edge1_(e typeA) ---> Node3_(n typeB)
Node1_(n typeA) --Edge1_(e typeB) ---> Node2_(n typeA)
# if u get me??

So my idea was to implement to build a database like this:

Node Types Table:
        primary_key, type
# where type relates to the name of a table holding node types, this is duplicated for edges

Relationships Table:
        primary_key, source_node_type, source_node_id, dest_node_type, dest_node_id, edge_type, edge_id

But the problem is I cant implement this in Django, is there a simple way of doing this, the code below is as far as I got in defining a model however I got stuck when implementing the Relationship table.

class Interval(models.Model): # a node
        start = models.IntegerField()
        end = models.IntegerField()

class Feature(models.Model): # a node
        name = models.TextField()

class SSaha(models.Model): # an edge
        score = models.IntegerField()

class Blast(models.Model): # an edge
         score = models.IntegerField()

class Node_Type(models.Model): # will contain (0, Interval) (1, Feature)
        type = models.TextField()

class Edge_Type(models.Model): # will contain (0, SSaha) (1, Blast)
        type = models.TextField()

class Relationship(models.Model):
        src_type = models.ForeignKey(Node_Type)
        src = models.ForeignKey()
        dest_type = models.ForeignKey(Node_Type)
        dest = models.FoeignKey()
        edge_type = models.ForeignKey(Edge_Type)
        edge = models.ForeignKey()

I m sorry if this isnt the best explanation. Any help would be gratefully appreciated.

Many Thanks
Nathan

Jeremy Dunck

unread,
Jan 15, 2007, 8:47:04 AM1/15/07
to django...@googlegroups.com
On 1/13/07, Nathan Harmston <ratch...@googlemail.com> wrote:
...

> class Relationship(models.Model):
> src_type = models.ForeignKey(Node_Type)
> src = models.ForeignKey()
> dest_type = models.ForeignKey(Node_Type)
> dest = models.FoeignKey()
> edge_type = models.ForeignKey(Edge_Type)
> edge = models.ForeignKey()
>
> I m sorry if this isnt the best explanation. Any help would be gratefully
> appreciated.

Consider making the node type a property of the node, and similarly
the edge type a prop of the edge. Then it's just a regular foreign
key, no? I guess this graph could have many edges per node pair and
might be undirected?

How big a graph are you talking about? I'm asking because there are
fairly well-developed graphing libraries, but Django's ORM isn't one
of them. :) If you just want to use it for display, have you
considered using Django's URLconf and templating, but not its ORM?

Reply all
Reply to author
Forward
0 new messages