How to access specific value from model choice field in django
Question:
So i have this material models which contain 3 values and can be selected from dropdown menu in the template, how can access this exact "tegangan_tarik" and "tegangan_values to be included in my output page
models.py
class Materials(models.Model):
SAE_Number = models.CharField(max_length=64)
tegangan_tarik = models.FloatField()
tegangan_luluh = models.FloatField()
def __str__(self):
return f"{self.SAE_Number}: Su = {self.tegangan_tarik} MPa; Sy = {self.tegangan_luluh} MPa"
forms.py
from django import forms
from .models import *
class MyForm(forms.Form):
N = forms.FloatField(label="Faktor Keamanan ",
widget=forms.NumberInput(attrs={'value': '2.0', 'min': '0'}))
T = forms.FloatField(label="Momen Puntir / Torsi ",
widget=forms.NumberInput(attrs={'placeholder': 'N-mm', 'min': '0'}))
Material = forms.ModelChoiceField(
label = "Pilih Bahan :", queryset = Materials.objects.all())
Ft = forms.FloatField(label="Gaya Tangensial Pada Elemen (Ft) ",
widget=forms.NumberInput(attrs={'placeholder': 'N', 'min': '0'}))
Fr = forms.FloatField(label="Gaya Radial Pada Elemen (Fr) ",
widget=forms.NumberInput(attrs={'placeholder': 'N', 'min': '0'}))
AB = forms.FloatField(label="Jarak Antara Bantalan A ke Elemen B (AB) ",
widget=forms.NumberInput(attrs={'placeholder': 'mm', 'min': '0'}))
BC = forms.FloatField(label="Jarak Antara Bantalan C ke Elemen B (BC) ",
widget=forms.NumberInput(attrs={'placeholder': 'mm', 'min': '0'}))
views.py
from django.shortcuts import render
from .forms import *
import math
# Create your views here.
def input(request):
return render(request, "shaft/input.html", {
"form": MyForm(),
})
def output(request):
N = float(request.POST['N'])
T = float(request.POST['T'])
Sy = #how to access this tegangan_tarik value from the models
Su = #how to access this tegangan_luluh value from the models
Ft = float(request.POST['Ft'])
Fr = float(request.POST['Fr'])
AB = float(request.POST['AB'])
BC = float(request.POST['BC'])
# Menghitung Gaya Reaksi Tumpuan
Az = (Ft * BC) / (AB + BC)
...
...
input.html
{% extends "shaft/layout.html" %}
{% load static %}
{% block title %}Input Form{% endblock %}
{% block body %}
<h1>INPUT PAGE</h1>
<form action="{% url 'shaft:output' %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit">
</form>
</body>
{% endblock %}
I dont really know what to do 🙁
Answers:
First, in the form you can also pass another variable like the id of a Materials instance.
In the output views receive that id like
id = request.POST["id"]
then import the Materials class on your views.py
material = Materials.objects.get(id=id)
then you can access all three attributes of that instance
material.tegangan_tarik
So i have this material models which contain 3 values and can be selected from dropdown menu in the template, how can access this exact "tegangan_tarik" and "tegangan_values to be included in my output page
models.py
class Materials(models.Model):
SAE_Number = models.CharField(max_length=64)
tegangan_tarik = models.FloatField()
tegangan_luluh = models.FloatField()
def __str__(self):
return f"{self.SAE_Number}: Su = {self.tegangan_tarik} MPa; Sy = {self.tegangan_luluh} MPa"
forms.py
from django import forms
from .models import *
class MyForm(forms.Form):
N = forms.FloatField(label="Faktor Keamanan ",
widget=forms.NumberInput(attrs={'value': '2.0', 'min': '0'}))
T = forms.FloatField(label="Momen Puntir / Torsi ",
widget=forms.NumberInput(attrs={'placeholder': 'N-mm', 'min': '0'}))
Material = forms.ModelChoiceField(
label = "Pilih Bahan :", queryset = Materials.objects.all())
Ft = forms.FloatField(label="Gaya Tangensial Pada Elemen (Ft) ",
widget=forms.NumberInput(attrs={'placeholder': 'N', 'min': '0'}))
Fr = forms.FloatField(label="Gaya Radial Pada Elemen (Fr) ",
widget=forms.NumberInput(attrs={'placeholder': 'N', 'min': '0'}))
AB = forms.FloatField(label="Jarak Antara Bantalan A ke Elemen B (AB) ",
widget=forms.NumberInput(attrs={'placeholder': 'mm', 'min': '0'}))
BC = forms.FloatField(label="Jarak Antara Bantalan C ke Elemen B (BC) ",
widget=forms.NumberInput(attrs={'placeholder': 'mm', 'min': '0'}))
views.py
from django.shortcuts import render
from .forms import *
import math
# Create your views here.
def input(request):
return render(request, "shaft/input.html", {
"form": MyForm(),
})
def output(request):
N = float(request.POST['N'])
T = float(request.POST['T'])
Sy = #how to access this tegangan_tarik value from the models
Su = #how to access this tegangan_luluh value from the models
Ft = float(request.POST['Ft'])
Fr = float(request.POST['Fr'])
AB = float(request.POST['AB'])
BC = float(request.POST['BC'])
# Menghitung Gaya Reaksi Tumpuan
Az = (Ft * BC) / (AB + BC)
...
...
input.html
{% extends "shaft/layout.html" %}
{% load static %}
{% block title %}Input Form{% endblock %}
{% block body %}
<h1>INPUT PAGE</h1>
<form action="{% url 'shaft:output' %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit">
</form>
</body>
{% endblock %}
I dont really know what to do 🙁
First, in the form you can also pass another variable like the id of a Materials instance.
In the output views receive that id like
id = request.POST["id"]
then import the Materials class on your views.py
material = Materials.objects.get(id=id)
then you can access all three attributes of that instance
material.tegangan_tarik