I am new to django and python, so forgive me if I make incorrect
references. I've been trying for days to find a solution. I will try
to give all the necessary details.
The general problem:
Accessing an attribute value of a foreign object through the form.
What I have:
A form that gathers information and upon submission adds a record to
the database and sends out an email. I am creating the form with
form_for_model. A basic version of the model is:
class Software_request(models.Model):
manager = models.ForeignKey(Manager)
fname = models.CharField("First name", max_length=15)
lname = models.CharField("Last name", max_length=15)
email = models.EmailField()
software = models.CharField(max_length=50)
class Admin:
list_display = ('lname', 'fname', 'software', 'manager')
list_filter = [''manager']
search_fields = ['software']
def __unicode__(self):
return '%s, %s' % (self.lname, self.fname)
In addition, you will notice the manager is a ForeignKey to this:
class Manager(models.Model):
fname = models.CharField("First name", max_length=15)
lname = models.CharField("Last name", max_length=15)
email = models.EmailField()
class Admin:
pass
def __unicode__(self):
return'%s, %s' % (self.lname, self.fname)
And of course my view looks like this:
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import render_to_response
from django.template import Context, loader
from django.core.mail import send_mail
from forms import RequestForm
def sw_request(request):
if request.method == 'POST':
form = RequestForm(request.POST)
if form.is_valid():
sender = form.cleaned_data['email']
# message = form['manager']
message = form.cleaned_data['manager']
send_mail(
'Software Request', message,
sender, ['[EMAIL PROTECTED]']
)
form.save()
return HttpResponseRedirect('/sw_request/thanks/')
else:
form = RequestForm()
return render_to_response('test2/sw_request.html', {'form': form})
def thanks(request):
return render_to_response('test2/thanks.html')
You can see I have a line commented out in the view. I have been
playing around with various ways of trying to get at the data. Once I
figure it out, I will be using the manager's email as a recipient
address. That's the only thing I cannot seem to get.
What I have noticed:
-The manager value in POST (when submitting the data) is the id number
of the respective manager record. This makes sense because the
software_request uses the id as the foreign key.
-The drop down field in the form for managers, displays the unicode
value of 'lastname, firstname' as expected(populated of course with
all the values in the table).
I have tried to set a variable to capture the manager's email but
cannot. I have tried various things (laugh as necessary) like:
mgr = form.manger.email
mgr = form.manager.get('email')
Usually the error page tells me that the 'manager' attribute does not
exist.
Is what I am attempting even possible? It seems very simple but no
matter what I try I'm not doing it right. Thanks to anyone who can
understand and help.
PS- ask as many additional questions if you want. I'll be working on
this all day...again :-\
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---