Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ForeignKey to abstract class (generic relations)

Tags:

I'm building a personal project with Django, to train myself (because I love Django, but I miss skills). I have the basic requirements, I know Python, I carefully read the Django book twice if not thrice.

My goal is to create a simple monitoring service, with a Django-based web interface allowing me to check status of my "nodes" (servers). Each node has multiple "services". The application checks the availability of each service for each node.

My problem is that I have no idea how to represent different types of services in my database. I thought of two "solutions" :

  • single service model, with a "serviceType" field, and a big mess with the fields. (I have no great experience in database modeling, but this looks... "bad" to me)
  • multiple service models. i like this solution, but then I have no idea how I can reference these DIFFERENT services in the same field.

This is a short excerpt from my models.py file : (I removed everything that is not related to this problem)

from django.db import models

# Create your models here.                                                                                                                          
class service(models.Model):
    port = models.PositiveIntegerField()
    class Meta:
        abstract = True

class sshService(service):
    username = models.CharField(max_length=64)
    pkey = models.TextField()   

class telnetService(service):
    username = models.CharField(max_length=64)
    password = models.CharField(max_length=64)

class genericTcpService(service):
    pass

class genericUdpService(service):
    pass

class node(models.Model):
    name = models.CharField(max_length=64)
    # various fields                                                                                                                                
    services = models.ManyToManyField(service)

Of course, the line with the ManyToManyField is bogus. I have no idea what to put in place of "*Service". I honestly searched for solutions about this, I heard of "generic relations", triple-join tables, but I did'nt really understand these things.

Moreover, English is not my native language, so coming to database structure and semantics, my knowledge and understanding of what I read is limited (but that's my problem)

like image 399
pistache Avatar asked Oct 25 '11 03:10

pistache


People also ask

What is a generic relation?

Generic relationships are abstraction patterns used for structuring information across application domains. They play a central role in information modeling.

WHAT IS models ForeignKey?

ForeignKey is a Django ORM field-to-column mapping for creating and working with relationships between tables in relational databases.

What is generic foreign key in Django?

Generic relations. Adding a foreign key from one of your own models to ContentType allows your model to effectively tie itself to another model class, as in the example of the Permission model above.

What is an abstract model Django?

An abstract model is a base class in which you define fields you want to include in all child models. Django doesn't create any database table for abstract models. A database table is created for each child model, including the fields inherited from the abstract class and the ones defined in the child model.


2 Answers

For a start, use Django's multi-table inheritance, rather than the abstract model you have currently.

Your code would then become:

from django.db import models

class Service(models.Model):
    port = models.PositiveIntegerField()

class SSHService(Service):
    username = models.CharField(max_length=64)
    pkey = models.TextField()   

class TelnetService(Service):
    username = models.CharField(max_length=64)
    password = models.CharField(max_length=64)

class GenericTcpService(Service):
    pass

class GenericUDPService(Service):
    pass

class Node(models.Model):
    name = models.CharField(max_length=64)
    # various fields                                                                                                                                
    services = models.ManyToManyField(Service)

On the database level, this will create a 'service' table, the rows of which will be linked via one to one relationships with separate tables for each child service.

The only difficulty with this approach is that when you do something like the following:

node = Node.objects.get(pk=node_id)

for service in node.services.all():
    # Do something with the service

The 'service' objects you access in the loop will be of the parent type. If you know what child type these will have beforehand, you can just access the child class in the following way:

from django.core.exceptions import ObjectDoesNotExist

try:
    telnet_service = service.telnetservice
except (AttributeError, ObjectDoesNotExist):
    # You chose the wrong child type!
    telnet_service = None

If you don't know the child type beforehand, it gets a bit trickier. There are a few hacky/messy solutions, including a 'serviceType' field on the parent model, but a better way, as Joe J mentioned, is to use a 'subclassing queryset'. The InheritanceManager class from django-model-utils is probably the easiest to use. Read the documentation for it here, it's a really nice little bit of code.

like image 116
Evan Brumley Avatar answered Sep 18 '22 15:09

Evan Brumley


I think one approach that you might consider is a "subclassing queryset". Basically, it allows you to query the parent model and it will return instances of the child models in the result queryset. It would let you do queries like:

models.service.objects.all() 

and have it return to you results like the following:

[ <sshServiceInstance>, <telnetServiceInstance>, <telnetServiceInstance>, ...]

For some examples on how to do this, check out the links on the blog post linked below.

http://jazstudios.blogspot.com/2009/10/django-model-inheritance-with.html

However, if you use this approach, you shouldn't declare your service model as abstract as you do in the example. Granted, you will be introducing an extra join, but overall I've found the subclassing queryset to work pretty well for returning a mixed set of objects in a queryset.

Anyway, hope this helps, Joe

like image 28
Joe J Avatar answered Sep 19 '22 15:09

Joe J