title: Django Nested admin
body: <p>here is my code:</p>
  
  <p>models.py</p>
  
  <pre><code>from django.db import models
  class Country(models.Model):
      country_name = models.CharField(max_length = 20, default = '', )
      country_other_details = models.CharField(max_length = 100, default = '', null = True)
      class Meta:
          verbose_name_plural = "Countries"
      def __str__(self):
          return self.country_name
  
  class State(models.Model):
      in_country = models.ForeignKey(Country, related_name='in_country',on_delete=models.DO_NOTHING)
      state_name = models.CharField(max_length = 20, default = '')
      state_other_details = models.CharField(max_length=100, default = '', null = True)
      def __str__(self):
          return self.state_name
  
  class City(models.Model):
      in_state = models.ForeignKey(State, related_name='in_state',on_delete=models.DO_NOTHING)
      city_name = models.CharField(max_length = 20, default = '')
      city_other_details = models.CharField(max_length = 100, null = True)
  
      class Meta:
          verbose_name_plural = "Cities"
  
      def __str__(self):
          return self.city_name
  </code></pre>
  
  <p>forms.py</p>
  
  <pre><code>from django.forms.models import inlineformset_factory
  from django.forms.models import BaseInlineFormSet
  
  from .models import Country, State, City
  
  from django.forms.models import BaseInlineFormSet, inlineformset_factory
  from django.utils.translation import ugettext_lazy as _
  
  # from publishing.utils.forms import is_empty_form, is_form_persisted
  
  CityFormset = inlineformset_factory(State, City, extra=2,  fields=("city_name",))
  
  
  class BaseStateFormset(BaseInlineFormSet):
      def add_fields(self, form, index):
          super(BaseStateFormset, self).add_fields(form, index)
  
          # save the formset in the 'nested' property
          form.nested = CityFormset(
              instance=form.instance,
              data=form.data if form.is_bound else None,
              files=form.files if form.is_bound else None,
              prefix='address-%s-%s' % (
                  form.prefix,
                  CityFormset.get_default_prefix()),
              # extra=1
          )
  
      def is_valid(self):
          result = super(BaseStateFormset, self).is_valid()
  
          if self.is_bound:
              for form in self.forms:
                  if hasattr(form, 'nested'):
                      result = result and form.nested.is_valid()
  
          return result
  
      def save(self, commit=True):
  
          result = super(BaseStateFormset, self).save(commit=commit)
  
          for form in self.forms:
              if hasattr(form, 'nested'):
                  if not self._should_delete_form(form):
                      form.nested.save(commit=commit)
  
          return result
  
  
  StateFormset = inlineformset_factory(Country, State, formset=BaseStateFormset, extra=2, fields=("state_name",))
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>from .models import Country, State, City
  
  def manage_state(request, parent_id):
  
  parent = get_object_or_404(Country, id=parent_id)
  
  if request.method == 'POST':
      formset = forms.StateFormset(request.POST, instance=parent)
      if formset.is_valid():
          formset.save()
          # return redirect('parent_view', parent_id=parent.id)
          return redirect(reverse('india:manage_state', kwargs={"parent_id": parent.id}))
  
  else:
      formset = forms.StateFormset(instance=parent)
  
  return render(request, 'home.html', {
                'parent':parent,
                'children_formset':formset})
  </code></pre>
  
  <p>What i want is a single form that will create an object to Parent(Country) model and multiple object to Child(State) as according to country's object and multiple objects to Grand Child(City) according to corresponded State</p>
  
  <p>for example:</p>
  
  <pre><code>&lt;form method="POST"&gt;
      Parentform:(Country)
          &lt;input name="country_name"&gt;
  
  
          Childform:(State)
              &lt;input name="state_name"&gt;
  
              GrandChildform:(City)
                  &lt;input name = "City_name"&gt;
  
              &lt;button&gt; Add GrandChild&lt;/button&gt;
  
          &lt;button&gt; Add Child &lt;/button&gt;
  
      &lt;button&gt;Add Parent&lt;/button&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>also the add button should be able to add the more Countries, States and Cities dynamically to the form.</p>
  
  <p>Any help, suggestions or references would be Grateful.
  Thanks in Advance.</p>