0

So I have the following files:

Views.py:

def home(request):
    return render(request, 'home/index.html')

def add(request):
    if request.method == 'POST': # If the form has been submitted...
        # ContactForm was defined in the previous section
        form1 = AddEventForm(request.POST) # A form bound to the POST data
        #person = AddPersonForm(request.POST) # A form bound to the POST data
        if form1.is_valid(): # All validation rules pass
            form = form1.save()
            request.session['event_data'] = request.POST
            #request.session['event_data2'] = form
            return HttpResponseRedirect('/add_person/')
            #return HttpResponseRedirect(reverse('/add_person', kwargs={'PK':form.pk}))
    else:
        form1 = AddEventForm() # An unbound form
        person = AddPersonForm() # A form bound to the POST data

    return render(request, 'add/add.html', {
        'form1': form1, 'person': person,
    })


def add_person(request):

    event_data = request.session.get('event_data')
    if request.method == 'POST':
        person = AddPersonForm(request.POST)
    else:
        person = AddPersonForm()

    return render(request, 'add_person/add_person.html',{'event_data': event_data, 'person': person,})

models.py:

class event(models.Model):
    event_name = models.CharField(max_length=200)
    date = models.DateField('Date',default=date.today)
    event_id = models.AutoField(primary_key=True)
    admin_username = models.CharField(max_length=50)

    def __unicode__(self):
        return ('%s %s %d') % (self.date, self.event_name, self.event_id)

class Person(models.Model):
    eventID = models.ForeignKey(event)
    first_name = models.CharField(max_length=50)
    second_name = models.CharField(max_length=50)
    email = models.EmailField(max_length=50)
    phone = models.CharField(max_length=25)
    company = models.CharField(max_length=200)
    favourite = models.BooleanField(default=False)

    def __unicode__(self):
        return eventID

This problem has been bugging me for hours. I have an initial page where the user enters some information about an event. If the checks pass the form saves and a primary key is generated. The user is then redirected to another page where they fill out a form about the people they have met there. The problem is I cannot find a way of passing the primary key to the second page (add_people). I need the primary key because it is my foreign key in my Person table and I need to use to pre-populate the eventID in the add_people page.

I initially thought the primary key would be in the request.POST after the form is saved but its not.

Harpal
  • 12,057
  • 18
  • 61
  • 74

3 Answers3

0

Query the table for the primary key using the form inputs as arguments. Once you get the key, pass it as a session variable in the request object.

Encinoman818
  • 231
  • 1
  • 2
  • 5
0

Here's an answer to the same question but with class-based views instead of function-based views: How do I pass a parent id as an fk to child object's ModelForm using generic class-based views in Django?. In that example, the foreign key is passed from the parent page to the child page by being a parameter in the URL. The child page catches it as a form parameter (in the form's __init__ function), stores it in a hidden field for display (by the View class's get_context_data and html code in the form template), and then submits it as part of the form.

Community
  • 1
  • 1
Joel Aufrecht
  • 442
  • 1
  • 5
  • 16
0

If your form is validated, then your have to set session value as eventID but not request.POST

if form1.is_valid(): # All validation rules pass
    form1.save(commit=False) 
    request.session['event_data'] = form1.cleaned_data['eventID']
    form1.save()
    return HttpResponseRedirect('/add_person/')
Chillar Anand
  • 27,936
  • 9
  • 119
  • 136