0
I am wanting to insert data into the database from a form, this form is working, but it contains many fields of type foreing key, so if I do not add the data previously they do not appear as an option in the form. So my question is how can I add data to these fields directly from the formulery? I wanted something like this when we are adding data in Django-admin, if the field is empty it is possible to add directly from that model. I don’t know if my explanation is clear.
here the models.py
class Add500(models.Model):
Kuni = models.ForeignKey(Country, on_delete=models.PROTECT, verbose_name='国')
MODELO_CH = (
('D25 SMOOTH', 'D25 SMOOTH'),
('D26 500W', 'D26 500W'),
)
Modelo = models.CharField(choices=MODELO_CH, max_length=20, verbose_name='機種')
Kiban = models.CharField(max_length=6, primary_key=True, verbose_name='機番')
TsukiBun = models.CharField(choices=TSUKI_BUN_CH, max_length=2)
SoBanBeO = models.ForeignKey(SousaBanBetsuOki, on_delete=models.PROTECT, verbose_name='操作盤別置')
C2JK = models.ForeignKey(Cable2JiKikan, on_delete=models.PROTECT, verbose_name='2次機関ケーブル')
Jb41 = models.ForeignKey(JB41, on_delete=models.PROTECT, verbose_name='JB41')
ChipCon = models.ForeignKey(ChipConveyor, on_delete=models.PROTECT, verbose_name='チップコン', null=True)
Jb52 = models.ForeignKey(JB52, on_delete=models.PROTECT, verbose_name='JB52')
Jb61 = models.ForeignKey(JB61, on_delete=models.PROTECT, verbose_name='JB61')
Painel = models.ForeignKey(Panel, on_delete=models.PROTECT, verbose_name='パネル')
opBox = models.ManyToManyField(OpBox, verbose_name='OP Box')
B2PC = models.ForeignKey(Box2PC, on_delete=models.PROTECT, verbose_name='2PC')
Trans = models.ForeignKey(Transformer, on_delete=models.PROTECT, verbose_name='トランス')
ToChu = models.ForeignKey(TokuChu, on_delete=models.PROTECT, verbose_name='特注')
BiKo = models.TextField(verbose_name='備考')
def __str__(self):
return self.Kiban
here the piece of Forms.py
class Toroku500(forms.ModelForm):
class Meta:
model = Add500
fields = '__all__'
here the views.py
def KibanToroku500(request):
if request.method == 'POST':
form500 = Toroku500(request.POST)
if form500.is_valid():
form = form500.save()
return redirect('/seiko/torokusentaku/')
else:
form500_class = Toroku500()
return render(request, 'seiko/add500_form.html', {'form500': form500_class})
It is working because I have added several values previously in the database, but in production the user will add the values according to the demand, so I would like a way to add directly from this form.
If anyone can help I appreciate!