如何在下拉選擇表單中輸出“id_no”和“name”[id_no - name] 組合。是否可以僅使用 {{form}} 直接對表單完成。
Model:
class Employee(models.Model):
id_no = models.CharField(unique = True,max_length=6)
name = models.CharField(max_length=100)
Form:
class EmployeeForm(forms.ModelForm):
class Meta:
model = Employee
fields = __all__
uj5u.com熱心網友回復:
是的,可以使用 {{form}} 實作str直接對表單完成:
class Employee(models.Model):
id_no = models.CharField(unique = True,max_length=6)
name = models.CharField(max_length=100)
def __str__(self):
return f"{self.id_no} - {self.name}"
但不是EmployeeForm因為這個表格沒有下拉選擇員工。您將在帶有外鍵到員工的表單的 modelFrom 上看到它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/384113.html
標籤:姜戈 django 模型 django-forms
