Another way is to make a common default Region
called 'New Region'
. When you inspect the POST-data, send the user to a create-new-region-view if the selected region is 'New Region'
.
This will be useful when the user want to create a new region when creating a location, independently of if there are any existing regions created already.
I wrote a simple example for you, see below. The idea is to add a hidden field to RegionForm
, so that the created Location
can be updated with the new region after the new region has been created. I'm not sure if this is the best way to solve the problem, but it works at very least (I set up a testing environment to test it).
Make sure that you create a 'Create New Region' Region
first, and set CREATE_NEW_REGION_ID
accordingly.
views.py
from django.http.response import HttpResponseRedirect
from django.shortcuts import render
from add_location.forms import LocationForm, RegionForm
# The ID of the 'Create New Region' region
CREATE_NEW_REGION_ID = 1
def add_location(request):
if request.method == 'POST':
display_form = LocationForm(request.POST)
region_form = RegionForm(request.POST)
# Check if the form is a valid LocationForm
if display_form.is_valid():
location = display_form.save()
# If region is the default 'Create new region' region, render the
# page with a new RegionForm
if location.region.id == CREATE_NEW_REGION_ID:
# Note that the location is saved above, so if the region isn't
# created in the next form, its region will stay
# 'Create New Region'
display_form = RegionForm(initial={'location': location})
else:
return HttpResponseRedirect('/thanks/')
# If not a valid LocationForm, check if it is a valid RegionForm
elif region_form.is_valid():
# Save the region, get the location and update the
# region of the location
region = region_form.save()
location = region_form.cleaned_data['location']
location.region = region
location.save()
return HttpResponseRedirect('/thanks/')
else:
display_form = LocationForm()
return render(request, 'form.html', {'form': display_form})
def thanks(request):
return render(request, 'thanks.html')
forms.py
from django import forms
from add_location.models import Location, Region
class LocationForm(forms.ModelForm):
class Meta:
model = Location
class RegionForm(forms.ModelForm):
location = forms.ModelChoiceField(Location.objects.all(),
widget=forms.HiddenInput())
class Meta:
model = Region
forms.html
<!DOCTYPE html>
<html>
<head>
<title>Thanks</title>
</head>
<body>
<form action="/add/location/" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
</body>
</html>
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'sampledjango.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^add/location/', 'add_location.views.add_location'),
url(r'^thanks/', 'add_location.views.thanks'),
)