Saturday, February 24, 2024

Re: Data model design questio: graph in database

-----BEGIN PGP SIGNATURE-----

wsB5BAABCAAjFiEE/NCg7Xf1UydoVFgpGvW31BqCOLMFAmXa7hUFAwAAAAAACgkQGvW31BqCOLOW
Qwf5Ab4OgM74aCG8AFfcFWLdsni38wErqVjcemgMDEOdFrqe4GYyfD0PrnetSD7uaW+oc1slINP3
kdMn+gLNzt7oVsXpQISIfnpU2bXJzxdv+RPnDuA2jLE3PBBtHy41DBf9hZP9Jm1q3Dbfuxk8adze
nzMEvoX/EVKS0vAv375Q9cWPQWFnDMMiMMMaOFO2aub5hQGZuHSN0dthKTBgTu9iyWDpYtMfbnT1
ux37qg4zI7BYFoEQS1ni1xY0ufN/iJurWJUSVgwikXOBYye8r67viUcWr2N3CHmkWeAy3OnHw55S
+jILUNuhHi/irEFHfAIMoZc0IOv/Mbo7yo9d+Xb5fw==
=Oz/E
-----END PGP SIGNATURE-----
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.  

No comments:

Post a Comment