On 24/02/2024 7:20 am, Sébastien Hinderer wrote:
Dear all,

Using the mailing list rather than the forum because, like many visually
impaired people, I find mailing lists way easier to use than forums, I
hope the list is still active.

I need to store in database and manipulate graphs of which both
vertices and edges can each be of several types.

At the moment I am modelling this as follows:

class Vertex(models.Model):
     pass

class Vertex_type_1(Vertex):
     # Data specific to vertices of type 1
     ...

class Vertex_type_2(Vertex):
     # Data specific to vertices of type 2
     ...

class Edge(models.Model):
     src = models.ForeignKey(Vertex,
       on_delete=models.CASCADE, related_name="+")
     dst = models.ForeignKey(Vertex,
       on_delete=models.CASCADE, related_name="+")
     class Meta:
         unique_together = ['src', 'dst']

class Edge_type_1(Edge):
     # Data specific to edges of type 1
     ...

class Edge_type_2(Edge):
     # Data specific to edges of type 2
     ...

I will have to wwite algorithms that work at the graph level, e.g.
to find paths. However, once a path has been found, at some point it
will become necessary to see which types of vertices and edges it is
that are involved in the path that has been found and I am wondering how
I will do that.

More concretely, with the current model, if an edge is found,
does Django have a way to also find out which type of edge it is,
i.e. which child class has given rise to that edge? Or do I need to add
type fields to the Vertex and Edge classes so that it becomes possible
to go from the parent to the children?

I feel unsure because on the one hand it feels to me it is necessary
with the current model to add such type fields, but on the other hand I
feel this is kind of wrong, almost like a code smell suggesting that I
am modelling things in a wrong way.

Any insight would be warmly appreciated as I am a total beginner with
Django.

In the base class you need a method like this ...

    def which_class(self):
        return self.__class__

... which will be inherited by each child class and when called will reveal the class name of the instance.

To test this in development, in the base class add a save method like this ...

    def save(self, *args, **kwargs):
        print(f"\n class name = {self.which_class()}")
        super().save(*args, **kwargs)

Whenever you save a model its name should print in stdout.  I tried it in one of my models and this is the result ...

    <class 'chemical.models.chemical.Chemical'>

... where "Chemical" is the class name.

So if you have a method which understands what the child class names mean, you can put your algorithm in there and execute calls to which_class() for each instance involved.

That might be enough to get you started.

Cheers

Mike


Many thanks in advance,

Seb.



--
We recommend signal.org

Signed email is an absolute defence against phishing. This email has
been signed with my private key. If you import my public key you can
automatically decrypt my signature and be sure it came from me. Your
email software can handle signing.

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/f81ef2aa-57eb-4cd8-bd53-679f9f68b143%40dewhirst.com.au.

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

Reply via email to