所以我有一个用模型Car
的外键调用的Manufacturer
模型。我还有CarCharacterisitcs
一个外键为Car
.
这是代码的样子:
class Car(models.Model):
idcar = models.AutoField(primary_key=True)
manufacturer = models.ForeignKey(Manufacturer, null=True, blank=True, on_delete=models.SET_NULL)
name = models.CharField(max_length=765)
class Manufacturer(models.Model):
idmanufacturer = models.AutoField(primary_key=True)
name = models.CharField(max_length=765)
class CarCharacteristics(models.Model):
idcar_characteristics = models.AutoField(primary_key=True)
car = models.ForeignKey(Car, on_delete=models.PROTECT)
我正在制作一个页面,让您添加CarCharacteristics
.
这是我的形式和观点:
class CarCharacteristicsForm(ModelForm):
class Meta:
model = CarCharacterisitics
def add_car_characteristics(request):
if request.method == 'POST':
car_char_form = CarCharacterisiticsForm(request.POST)
if car_char.is_valid(:
car_char_form.save()
return HttpResponseRedirect('/cars/car-characterisitcs')
else:
car_char_form = CarCharacterisiticsForm()
return render(request, 'car_characteristics/add_car_characteristics.html',{'car_char_form': car_char_form,})
在我的 html 页面中,我有这个:
<h1>Car Characteristics</h1>
<form action="." method="POST">
{% csrf_token %}
<table id="characteristics_table">
<tr>
<td>
<table id="car_characterisitcs_table">
<td>{{ char_char_form.as_table }}</td>
</table>
</td>
</tr>
</table>
<p><input type="submit" value="Submit"></p>
</form>
Car
显示此表单时,我有一个包含所有可能模型的下拉选择字段。我想要的是有两个选择字段。我希望第一个让您能够选择Manufacturer
,然后第二个显示所有可能Cars
的制造商。然后,当您提交表单时,它将Car
您选择的分配给模型的外键CarCharacterisitcs
。
如果你去看看Advance Auto Parts,当你点击“你的车”时,这就是我想要的。我怎么做?