'How do I fill automatically a foreign key on a form of multiple pages in Django? (function based view)

I'm developing a website where the user have to compile a form to start an equipment purchase procedure. In my django website I've created several models with the Foreign Key referred to the principal model which is called MAIN.

In particular I have these two models: MAIN and SPECIFICATIONS. SPECIFICATIONS has a foreign key which is ID_request and it connects to the MAIN model.

models.py

class MAIN(models.Model):
 ## attributes

class SPECIFICATIONS(models.Model):
    ID_request = models.ForeignKey(MAIN, on_delete=models.CASCADE)
    Spec = models.CharField(max_length=100, blank=False)
    CliReas = models.CharField(max_length=100, blank=False)
    rif = models.IntegerField(default=0, null=False)
    MinMax = models.CharField(max_length=20, blank=True)

I'm structuring the form into two pages, where in the first one the user has to compile some fields of the MAIN model. In the second one I would like to make the user fill 'Spec', 'CliReas', 'rif', 'MinMax' fields, but I want also the ID_request be automatically set to that of the previously entered device. Can you suggest me how I can do it?

I'm using function-based views:

views.py

def new_2_4(request):
    form = SpecForm()

    if request.method=='POST':
        form1 = SpecForm(request.POST)
        if form1.is_valid():
            form1.save()
            return redirect('/')

    context={'form1': form1}
    return render(request, 'new_2_4.html', context)

And forms.py

class SpecForm(ModelForm):
    class Meta:
        model = SPECIFICATIONS
        fields = ['ID_request', 'rif','Spec', 'CliReas', 'MinMax']

Thank you in advance



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source