I'm trying to pass a primary key variable straight into a form (foreign key) so the user doesn't have to select it. This is what I have:
I have an HTML button
<a href="{% url 'addfraction' pk=botany.botany_id %}" class="btn btn-primary" role="button">Add Fraction</a>
It passes the primary key of the botany table (botany_id) through the urls.py
url(r'^addfraction/(?P<pk>\d+)/$', views.addfraction, name='addfraction'),
The pk comes in to the def addfraction
def addfraction(request, pk):
if request.method == "POST":
form = FractionForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.save()
return redirect('allbotany')
else:
form = FractionForm()
return render(request, 'fraction/create_fraction.html', {'form': form})
And the data in the form is saved to the database.
class FractionForm(forms.ModelForm):
class Meta:
model = Fraction
fields = (
#'fraction_id',
'botany_id',
'proportion_analysed',
'soil_volume',
'sample_volume',
'sample_weight',
)
botany_id is a foreign key, it becomes a dropdown box, this needs to be greyed out and contain the botany_id pk. Where have I gone wrong? Any pointers welcome.