tags:

views:

50

answers:

2

Given the following models:

class BaseMachine(models.Model)
    fqdn = models.CharField(max_length=150)
    cpus = models.IntegerField()
    memory = models.IntegerField()

    class Meta:
        abstract = True

class PhysicalMachine(BaseMachine)
    location = models.CharField(max_length=150)


class VirtualMachine(BaseMachine)
    hypervisor = models.CharField(max_length=5)


class Sysadmin(models.Model):
    name = models.CharField(max_length=100)
    admin_of = models.ManyToManyField...

In this example I would like to relate 1 sysadmin to many machines - be them either an instance or PhysicalMachine or VirtualMachine. I know I can't have a ManyToMany with an abstract base, but I was wondering if there was a better way of achieving this than just having a separate ManyToMany field on sysadmin for each of the models? In this small example that could be tolerable, but if you have more than 2 subclasses, or if there are other models which you need to relate with the 'base' class, it becomes something more to manage.

Thanks :)

+2  A: 

Have you considered a generic relationship using the contenttypes framework?

Manoj Govindan
+1  A: 

EDIT: I have updated the soultion, so one admin can have many machines and one machine can have many admins:

class Sysadmin(models.Model):
    name = models.CharField(max_length=100)


class BaseMachine(models.Model):
    fqdn = models.CharField(max_length=150)
    cpus = models.IntegerField()
    memory = models.IntegerField()
    admins = models.ManyToManyField(Sysadmin)

    class Meta:
        abstract = True

class PhysicalMachine(BaseMachine):
    location = models.CharField(max_length=150)


class VirtualMachine(BaseMachine):
    hypervisor = models.CharField(max_length=5)



In [1]: s1 = Sysadmin(name='mike')

In [2]: s1.save()

In [3]: m1 = PhysicalMachine(fqdn='test', cpus=1, memory=20, location='test')

In [4]: m1.save()

In [5]: m1.admins.add(s1)

In [6]: m1.save()

In [7]: m2 = VirtualMachine(fqdn='test', cpus=1, memory=20, hypervisor='test')

In [8]: m2.save()

In [9]: m2.admins.add(s1)

In [10]: m2.save()

In [11]: m1.admins.all()
Out[11]: [<Sysadmin: Sysadmin object>]

In [12]: m2.admins.all()
Out[12]: [<Sysadmin: Sysadmin object>]
Dominik Szopa
But could I attach many *machine's to 1 sysadmin instance? Using ContentType I would have to create multiple sysadmin instances for each *machine?
Darren
I have updated the solution, so one admin can have many machines and one machine can have many admins.
Dominik Szopa
Reversing the relationship will work for my use case, thanks guys.
Darren
If you find my answer useful, please mark it as correct with green mark. Thank you.
Dominik Szopa