Django ManytoMany Relationships

218 views
Skip to first unread message

G Z

unread,
Jul 15, 2014, 1:44:18 PM7/15/14
to django...@googlegroups.com

I will start by saying I have an existing database, for the purpose of my question it has three tables:
Vm
Vm_licenses
Licenses

This database is per-existing before we decided to make a django web-portal. Basically licenses is a database that contains all possibile licenses, and Vm contains all the vms under our architecture. We then have a table vm_licenses, where it has a unique index vm_license_id, and two other columns vm_id with is the primary key for VM and license_id which is the primary key for licenses. I want to use the admin page to be able to edit the vm_licenses that way when i select a vm from vm_licenses on the admin page I can select from a list of licenses which licenses go with that vm. The only problem is the database already exist and as far as I can tell the relationship between the tables doesn't exist in a way django can do this with.

I have the following model set up:

class License(models.Model):
   license_id = models.BigIntegerField(primary_key = True, editable = False, db_column = 'license_id')
   license_authority = models.ForeignKey(License_authoritie,  on_delete = models.PROTECT)
   product = models.CharField(max_length = 20)
 
   class Meta:
      managed = False
      db_table = 'licenses'
      ordering = ['product']

   def __unicode__(self):  # Python 3: def __str__(self):
      return self.product
        
class Vm_license(models.Model):
   vm_license_id = models.BigIntegerField(primary_key = True, editable = False, db_column = 'vm_license_id')
   license= models.ForeignKey(License,  on_delete = models.PROTECT)
   vm = models.ForeignKey(Vm,  on_delete = models.PROTECT)
  
   class Meta:
      managed = False
      db_table = 'vm_licenses'


class Vm(models.Model):
   vm_id = models.BigIntegerField(primary_key = True, editable = False, db_column = 'vm_id')
   vm_mor = models.CharField(max_length = 20)
   vm_name = models.CharField(max_length = 256)
   vm_group= models.ForeignKey(Vm_group,  on_delete = models.PROTECT)
   vm_display_name = models.CharField(max_length = 30)
   datacenter= models.ForeignKey(Datacenter,  on_delete = models.PROTECT)
   vcd_managed = models.CharField(max_length = 60)

   class Meta:
      managed = False
      db_table = 'vms'

   def __unicode__(self):  # Python 3: def __str__(self):
      return self.vm_name

My admin page is :

class vm_license_admin(admin.ModelAdmin):
    list_display = ('vm', 'vm_license_id')
    search_fields = ('vm__vm_name',)
    ordering = ('vm',)
    #filter_horizontal = ('license',)


it says I need a manytomany relationship so I cahnged 
 
license= models.ForeignKey(License,  on_delete = models.PROTECT)

license= models.ManyToManyField(License)

but then it says it cant find the table or view from oracle when I change it..


How do I fix this I want to be able to assign licenses like the following picture:


C. Kirby

unread,
Jul 15, 2014, 2:01:29 PM7/15/14
to django...@googlegroups.com
ManyToMany fields actually create a new intermediary join table in the database. If you are using an unmanaged database then you will need to create the intermediary table yourself. See the ManyToMany field reference

G Z

unread,
Jul 15, 2014, 3:23:01 PM7/15/14
to django...@googlegroups.com
Kirby,

This is what i have so far but i dont understand how to get the table to do what I want. Because all I get on vm_licenses is the ability to select the vm there is no licenses anymore. I'm not quite sure how all of this works together.

class License(models.Model):
   license_id = models.BigIntegerField(primary_key = True, editable = False, db_column = 'license_id')
   license_authority = models.ForeignKey(License_authoritie,  on_delete = models.PROTECT)
   product = models.CharField(max_length = 20)
  
  
   class Meta:
      managed = False
      db_table = 'licenses'
      ordering = ['product']

   def __unicode__(self):  # Python 3: def __str__(self):
      return self.product
        
class Vm_license(models.Model):
   vm_license_id = models.BigIntegerField(primary_key = True, editable = False, db_column = 'vm_license_id')
   license= models.ManyToManyField(License, through="vm_assignments")
   vm= models.ForeignKey(Vm,  on_delete = models.PROTECT)
   #vm = models.ForeignKey(Vm,  on_delete = models.PROTECT)

  
   class Meta:
      managed = False
      db_table = 'vm_licenses'
     
class vm_assignments(models.Model):
   vm_license = models.ForeignKey(Vm_license,  on_delete = models.PROTECT)
   license = models.ForeignKey(License,  on_delete = models.PROTECT)

G Z

unread,
Jul 15, 2014, 3:32:53 PM7/15/14
to django...@googlegroups.com
Admin.py

class vm_license_admin(admin.ModelAdmin):
    list_display = ('vm',)

    search_fields = ('vm__vm_name',)
    ordering = ('vm',)
    filter_horizontal = ('license',)
   
admin.site.register(Vm_license, vm_license_admin)




class License(models.Model):
   license_id = models.BigIntegerField(primary_key = True, editable = False, db_column = 'license_id')
   license_authority = models.ForeignKey(License_authoritie,  on_delete = models.PROTECT)
   product = models.CharField(max_length = 20)
  
  
   class Meta:
      managed = False
      db_table = 'licenses'
      ordering = ['product']

   def __unicode__(self):  # Python 3: def __str__(self):
      return self.product
        
class Vm_license(models.Model):
   vm_license_id = models.BigIntegerField(primary_key = True, editable = False, db_column = 'vm_license_id')
   license= models.ManyToManyField(License, through="vm_assignments")
   vm= models.ForeignKey(Vm,  on_delete = models.PROTECT)


  
   class Meta:
      managed = False
      db_table = 'vm_licenses'
     
class vm_assignments(models.Model):

   vm = models.ForeignKey(Vm,  on_delete = models.PROTECT)
   vm_license = models.ForeignKey(Vm_license,  on_delete = models.PROTECT)
   license = models.ForeignKey(License,  on_delete = models.PROTECT)


but the only thing that shows on vm_licenses is the vm there is no manytomany form element like im trying to get im simply mis understanding this
Reply all
Reply to author
Forward
0 new messages