Multiple Model Form Generation

53 views
Skip to first unread message

G Z

unread,
May 15, 2014, 11:55:47 AM5/15/14
to django...@googlegroups.com
I want to call three models into one form on my page but it doens't like the way that i am doing it. Any help would be awesome if i call them idividually it just displaces the last one i call.



from django import forms
from .models import Customer, Vms, Vmspecs

class SignUpForm(forms.ModelForm):
        class Meta:
                model = (Customer, Vms, Vmspecs)

so thats my code but it doesnt work it gives me the following error





Environment:


Request Method: GET

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
  101.                 resolver_match = resolver.resolve(request.path_info)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
  337.             for pattern in self.url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in url_patterns
  365.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in urlconf_module
  360.             self._urlconf_module = import_module(self.urlconf_name)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
  40.         __import__(name)
File "/root/djangoprojects/provisioning/provisioning/urls.py" in <module>
  7. url(r'^customers/', include('vmware.urls')),
File "/usr/local/lib/python2.7/dist-packages/django/conf/urls/__init__.py" in include
  26.         urlconf_module = import_module(urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
  40.         __import__(name)
File "/root/djangoprojects/provisioning/vmware/urls.py" in <module>
  5. from vmware import views
File "/root/djangoprojects/provisioning/vmware/views.py" in <module>
  6. from .forms import SignUpForm
File "/root/djangoprojects/provisioning/vmware/forms.py" in <module>
  4. class SignUpForm(forms.ModelForm):
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in __new__
  282.                                       opts.help_texts, opts.error_messages)
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in fields_for_model
  175.     opts = model._meta

Exception Type: AttributeError at /
Exception Value: 'tuple' object has no attribute '_meta'

G Z

unread,
May 15, 2014, 12:31:32 PM5/15/14
to django...@googlegroups.com
so I changed it a bit after doing some more research however formb, and formc don't display, if I change the 'formb' to 'form' it will overwrite the first one and only display the second one.
This is how i read the forignekey associate between multiple forms to be. 


from django.shortcuts import render
from django.http import HttpResponse
from vmware.models import Customer
from django.shortcuts import render_to_response
from vmware.models import Vms
from .forms import SignUpForm
from .forms import VmForm
from .forms import VmSpecForm
from vmware.models import Vmspecs
from django.views.decorators.csrf import csrf_protect
from django.template import Template, RequestContext

#@csrf_protect
def index(request):
        form = SignUpForm(request.POST or None)
        formb = VmForm(request.POST or None)
        formc = VmSpecForm(request.POST or None)

        if form.is_valid():
                save_it = form.save(commit=False)
                save_it.save()
                save_itb = formb.save(commit=False)
                save_itc = formc.save(commit=False)
                save_itb.CUSTOMERID = save_it
                save_itb.save()
                save_itc.VMID = save_itb
                save_itc.CUSTOMERID = save_it
                save_itc.save()

        customers = Customer.objects.all()
        ctx = { 'customers':customers, 'form':form, 'formb':formb, 'formc':formc}
        return render_to_response('index.html', ctx, context_instance=RequestContext(request)) 


<form action='' method='POST'> {% csrf_token %}
     {{ form.as_p }}
     {{ formb.as_P }}
     {{ formc.as_P }}
     <p><input type="submit" value="Add"></p>
</form>


Message has been deleted

Brendan Edwards

unread,
May 15, 2014, 6:12:34 PM5/15/14
to django...@googlegroups.com
Why not just do this? (Sorry for the crappy indentation.. tab doesn't work on here for some reason)

models.py:
 
class SignUpForm(forms.ModelForm):
        class Meta:
                model = Customer
 
class VmForm(forms.ModelForm):
        class Meta:
                model = Vms

class VmSpecForm(forms.ModelForm):
        class Meta:
                model = Vmspecs 

views.py

def index(request):
       if request.POST:
        form = SignUpForm1(request.POST or None)
        formb = VmForm(request.POST or None)
        formc = VmSpecForm(request.POST or None)

        if form.is_valid() and formb.is_valid() and formc.is_valid()
                form.save()
                formb.save()
                formc.save()
else

load template and forms etc..

G Z

unread,
May 16, 2014, 4:25:06 PM5/16/14
to django...@googlegroups.com
Does that work if they all have forignekey associations with each other?

G Z

unread,
May 16, 2014, 4:36:36 PM5/16/14
to django...@googlegroups.com
I tried it it doesn't work because they keys don't associate when you click save.
Reply all
Reply to author
Forward
0 new messages