Django Linking Multiple Mysql tables with unique identifiers

26 views
Skip to first unread message

G Z

unread,
May 6, 2014, 5:34:51 PM5/6/14
to django...@googlegroups.com
So I'm trying to set up my django app and I want to link multiple tables each with a unique key identifier. To test this i have the following models. py
<code>
from django.db import models

# Create your models here.
class Customer(models.Model):
    NAME = models.CharField(max_length=200)
    WEBSITE = models.CharField(max_length=200)
    PHONE = models.CharField(max_length=200)
    EMAIL = models.CharField(max_length=200)
    ADDRESS = models.CharField(max_length=200)
    VMIDS = models.CharField(max_length=200)

    def __unicode__(self):
        return self.NAME
#               return self.NAME

class Vms(models.Model):
    VMID  = models.CharField(max_length=200)
    VMNAME = models.CharField(max_length=200)
    VMSTATUS = models.CharField(max_length=200)
    CUSTOMERID = models.ForeignKey(Customer)

    def __unicode__(self):
        return self.VMNAME
class Vmspecs(models.Model):
    CPUS =  models.CharField(max_length=200)
    CORES =  models.CharField(max_length=200)
    MEMORY =  models.CharField(max_length=200)
    HDSPACE =  models.CharField(max_length=200)
    OS =  models.CharField(max_length=200)
    UPTIME = models.CharField(max_length=200)
    VMID = models.ForeignKey(Vms)

    def __unicode__(self):
        return self.VMID
</code>

If works fantastic until I try to add a vmspecs in the admin panel and link it to a VM it gives the following error





<code>

Environment:


Request Method: POST

Django Version: 1.6.4
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'vmware')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in wrapper
  432.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
  99.                     response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
  52.         response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py" in inner
  198.             return view(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapper
  29.             return bound_func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
  99.                     response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in bound_func
  25.                 return func(self, *args2, **kwargs2)
File "/usr/local/lib/python2.7/dist-packages/django/db/transaction.py" in inner
  371.                 return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in add_view
  1133.                 self.log_addition(request, new_object)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in log_addition
  600.             action_flag=ADDITION
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/models.py" in log_action
  19.         e = self.model(None, None, user_id, content_type_id, smart_text(object_id), object_repr[:200], action_flag, change_message)

Exception Type: TypeError at /admin/vmware/vmspecs/add/
Exception Value: 'Vms' object has no attribute '__getitem__'
</code>

G Z

unread,
May 6, 2014, 10:06:16 PM5/6/14
to django...@googlegroups.com
Easier to be red

Ok so I have three tables in my database Customer Vms and Vmspecs

Vms is tied through customer by customer id through database id field and vmspecs is tied to vms through the id field for Vms. However when I go into admin panel and try to add vmspecs to vms after connecting the vm to a customer i get the and error here is my model contorller.

Here is there error

G Z

unread,
May 7, 2014, 3:37:06 PM5/7/14
to django...@googlegroups.com
Anyone?

Daniel Roseman

unread,
May 7, 2014, 6:34:19 PM5/7/14
to django...@googlegroups.com
The trouble is in your VMSpec model's `__unicode__` method. That actually needs to return unicode, but you are returning the value of the VMID ForeignKey field, which is a VMS object. You should either return `unicode(self.VMID)`, or choose another field to be the unicode representation.
--
DR.

G Z

unread,
May 7, 2014, 8:57:39 PM5/7/14
to django...@googlegroups.com
Thanks that worked.


On Tuesday, May 6, 2014 11:34:51 AM UTC-6, G Z wrote:
Reply all
Reply to author
Forward
0 new messages