How do i call a foreign key from another view in another form in django?

I have a stocks model that relies on GRN (Goods Receiving Note) model primary key in order to store stocks of a certain GRN in the database. I would like to humbly ask for guidance on how i can click a GRN and enter stocks in a form with the open GRN No already selected in this form.

Below are my views:

 def grns(request):
    grns = GRN.objects.all()
    return render (request, 'products/GRNS.html', {'grns' : grns})


def grn(request, pk):
    gnrObj = GRN.objects.get(GRN_id = pk)
    return render(request, 'products/stocks_in.html', {'grn': gnrObj})


def createGrn(request):
    form = GrnForm()

    if request.method == 'POST':
        form = GrnForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('grns')

    context = {'form': form}
    return render (request, 'products/grn_form.html', context)
    

def stocks(request):
    stocks = Stock.objects.all()
    return render (request, 'products/stocks_in.html', {'stocks' : stocks})


def stock(request, pk):
    stockObj = stock.objects.get(GRN_id = pk)
    return render(request, 'products/stocks_in_form.html', {'stock': stockObj})


def createStock(request):
    form = StockForm()

    if request.method == 'POST':
        form = StockForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('stocks')

    context = {'form': form}
    return render (request, 'products/stocks_in_form.html', context)

Below are my urls:

#GRNS Urls
path('grns', views.grns, name = "grns"),
path('create-grn/', views.createGrn, name = "create-grn"),
path('grn/<str:pk>/', views.grn, name = "grn"),


#Stocks Urls
path('stocks', views.stocks, name = "stocks"),
path('create-stock/', views.createStock, name = "create-stock"),
path('stock/<str:pk>/', views.stock, name = "stock"),

Below are my forms:

class GrnForm(ModelForm):
class Meta:
    model = GRN
    fields = ['GRN_no', 'date', 'supplier']

def __init__(self, *args, **kwargs):
    super(GrnForm, self).__init__(*args, **kwargs)
    

    self.fields['GRN_no'].widget.attrs.update(
            {'class': 'form-control'}
            )

    self.fields['date'].widget.attrs.update(
            {'class': 'form-control', 'type': 'date'}
            )

    self.fields['supplier'].widget.attrs.update(
            {'class': 'form-control'}
            )

One common way to do this is to include the pk for the GRN Number as a parameter in the url for your form page. Your first page then would show the GRN Numbers and be links to this second page, where your template rendered the pk of that GRN Number as part of the url for that link.

You don’t show your models here so it’s tough to get more specific than this. It’s not clear to me from your description whether there’s a 1-1 or a 1-Many relationship between Stock and GRN. If the latter, then you may want to look at using an Inline formset to help manage that second page.