Issue
I've create a view and in that view I declared variable for Address Model. Now when I am calling forms built on these models it is showing me the error. I used ForeignKey for user and in form User field was popping up multiple times. So I also need a simplest solution to merge a single form with multiple models with ForeignKeys. Here is my View.
def edit_profile(request):
user = request.user
address = Address.objects.get(user=user)
picture = UserPicture.objects.get(user=user)
job = Job.Objects.get(user=user)
address_form = AddressForm(request.POST or None, instance=address)
job_form = JobForm(request.POST or None, instance=job)
user_picture_form = UserPictureForm(request.POST or None, instance=picture)
return render_to_response('profiles/edit_profiles.html', locals(),\n
context_instance=RequestContext(request))
Form is something like:
from django import forms
from .models import Job, Address, UserPicture
class AddressForm(forms.ModelForm):
class Meta:
model = Address
class JobForm(forms.ModelForm):
class Meta:
model = Job
class UserPictureForm(forms.ModelForm):
class Meta:
model = UserPicture
Template has following:
{% extends 'base.html' %}
{% block content %}
<div class="row">
{{ address_form.as_p }}
</div>
<div class="row">
{{ job_form.as_p }}
</div>
<div class="row">
{{ user_picture_form.as_p }}
</div>
{% endblock %}
My models are:
from django.db import models
from django.contrib.auth.models import User
class Address(models.Model):
user = models.ForeignKey(User)
street_address = models.CharField(max_length=200)
city = models.CharField(max_length=200)
state = models.CharField(max_length=200)
zip_code = models.IntegerField(max_length=6)
phone = models.CharField(max_length=20, null=True, blank=True)
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
active = models.BooleanField(default=True)
def __unicode__(self):
return self.city
class Job(models.Model):
user = models.ForeignKey(User)
position = models.CharField(max_length=100)
employer = models.CharField(max_length=200)
employer_address = models.CharField(max_length=200)
city = models.CharField(max_length=200)
state = models.CharField(max_length=200)
zipcode = models.IntegerField(max_length=6)
phone = models.CharField(max_length=20, null=True, blank=True)
start_date = models.DateField(auto_now=False, auto_now_add=False)
end_date = models.DateField(auto_now=False, auto_now_add=False)
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
active = models.BooleanField(default=True)
def __unicode__(self):
return self.position
class UserPicture(models.Model):
user = models.ForeignKey(User)
image = models.ImageField(upload_to='profiles/')
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
active = models.BooleanField(default=True)
def __unicode__(self):
return str(self.image)
Error is Below:
DoesNotExist at /edit/
Address matching query does not exist.
Request Method: GET
Request URL: http://localhost:8000/edit/
Django Version: 1.6.8
Exception Type: DoesNotExist
Exception Value:
Address matching query does not exist.
Exception Location: C:\Users\Mudassar\AppData\Roaming\Python\Python27\site-packages\django\db\models\query.py in get, line 310
Python Executable: C:\Python27\python.exe
Python Version: 2.7.9
Python Path:
['C:\\Users\\Mudassar\\PycharmProjects\\matcher',
'C:\\windows\\SYSTEM32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Users\\Mudassar\\AppData\\Roaming\\Python\\Python27\\site-packages',
'C:\\Python27\\lib\\site-packages',
'C:\\Python27\\lib\\site-packages\\PIL']
Server time: Wed, 12 Aug 2015 23:50:55 +0500
Traceback Switch to copy-and-paste view
C:\Users\Mudassar\AppData\Roaming\Python\Python27\site-packages\django\core\handlers\base.py in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
C:\Users\Mudassar\PycharmProjects\matcher\profiles\views.py in edit_profile
address = Address.objects.get(user=user) ...
▶ Local vars
Advise requested.
Solution
You need to write a few conditionals it appears. You need to create these objects before you can get them so first create them like so. Then once the objects are created you can do what you where trying to do, get_or_create would not work well here because you want to give your user a form to enter their profile information, even though you could set defaults but this is much simpler to understand.
import django.shortcuts import render
def add_profile(request):
context = {}
if request.method == 'POST':
address_form = AddressForm(request.POST)
job_form = JobForm(request.POST)
user_picture_form = UserPictureForm(request.POST)
if address_form.is_valid() and job_form.is_valid() and user_picture_form.is_valid():
adress_form.save()
job_form.save()
user_picture_form.save()
else:
print adress_form.errors, job_form.errors, user_picture_form.errors
else:
context['address_form'] = AddressForm()
context['job_form'] = JobForm()
context['user_picture_form'] = UserPictureForm()
return render(request, 'profiles/add_profiles.html', context)
def edit_profile(request):
context = {}
user = request.user
adress = Adress.objects.get(user=user)
job = Job.objects.get(user=user)
picture = UserPicture.objects.get(user=user)
if request.method == 'POST':
address_form = AddressForm(request.POST, instance=adress)
job_form = JobForm(request.POST, instance=job)
user_picture_form = UserPictureForm(request.POST, instance=picture)
if address_form.is_valid() and job_form.is_valid() and user_picture_form.is_valid():
adress_form.save()
job_form.save()
user_picture_form.save()
else:
print adress_form.errors, job_form.errors, user_picture_form.errors
else:
context['address_form'] = AddressForm(instance=adress)
context['job_form'] = JobForm(instance=job)
context['user_picture_form'] = UserPictureForm(instance=picture)
return render(request, 'profiles/edit_profiles.html', context)
Answered By - user4409008
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.