Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6737625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:16:16+00:00 2026-05-26T11:16:16+00:00

I’m building a personal project with Django, to train myself (because I love Django,

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:16:16+00:00Added an answer on May 26, 2026 at 11:16 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.