Django step by step form with multiple ForeingKey fields

I can't make a step-by-step form for filling related models. If you want to suggest FormWizard, I can't figure it out. There are very few examples on the Internet and the examples are too simple. I'm trying to use a user session and save forms gradually

class City(models.Model):
    obl = models.CharField(max_length=255, choices=REGIONS, default="24", verbose_name="Регион")
    name = models.CharField(max_length=128, verbose_name="Город")
    population = models.IntegerField()

class Address(models.Model):
    city = models.ForeignKey(City, on_delete=models.PROTECT, verbose_name="Город")
    street = models.CharField(max_length=255, verbose_name="Улица")
    numb = models.CharField(max_length=64, verbose_name="Номер дома")

class Project(models.Model):
    manager = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name="Сотрудник")
    address = models.ForeignKey(Address, on_delete=models.PROTECT, verbose_name="Адрес")

    # about 30 fields

    accept = models.DateField(verbose_name="Принят дата", blank=True)

class ProjectDetail(models.Model):
    project = models.OneToOneField(Project, on_delete=models.CASCADE, verbose_name="Проект")
    img = models.ImageField(upload_to=("%d-%m-%Y/"), verbose_name="Обложка объекта")

    # about 40 fields

    lessor = models.CharField(max_length=255, verbose_name="Контактное лицо")
    lessor_phone = models.PositiveBigIntegerField(verbose_name="Телефон")

Forms

class AddressForm(forms.ModelForm):
    class Meta:
        model = Address
        fields = ["city", "street", "numb"]

    def __init__(self, *args, **kwargs):
        city = kwargs.pop("city", "")
        super(AddressForm, self).__init__(*args, **kwargs)
        self.fields["city"] = forms.ModelChoiceField(queryset=City.objects.all())


class ProjectForm(forms.ModelForm):
    class Meta:
        model = Project
        fields = ["file", "sq", "rent_tax"]

class ProjectDetailForm(forms.ModelForm):
    class Meta:
        model = ProjectDetail
        exclude = ['project', 'comment', 'resume', 'complite', 'created_at']

Views

def step1(request):
    initial={'fn': request.session.get('fn', None)}
    print(initial)
    form = AddressForm(request.POST or None, initial=initial)
    if request.method == 'POST':
        if form.is_valid():
            request.session['fn'] = form.cleaned_data['fn']
            return HttpResponseRedirect(reverse('step2'))
    return render(request, 'crm/step1.html', {'form': form})

def step2(request):
    form = ProjectForm(request.POST or None)
    if request.method == 'POST':
        if form.is_valid():
            project = form.save(commit=False)
            address = Address.objects.create(fn=request.session['fn'])
            project.address = address
            request.session['fn'] = form.cleaned_data['fn']            
            project.save()
            return HttpResponseRedirect(reverse('step3'))
    return render(request, 'crm/step2.html', {'form': form})

def step3(request):
    form = ProjectDetailForm(request.POST or None)
    if request.method == 'POST':
        if form.is_valid():
            proj_det = form.save(commit=False)
            project = Project.objects.create(fn=request.session['fn'])
            proj_det.project = project
            proj_det.save()
            return HttpResponseRedirect(reverse('finished'))
    return render(request, 'crm/step2.html', {'form': form})

The problem is initial={'fn': request.session.get('fn', None)} Django can't get a session

Back to Top