0
我有兩個通過ForiegnKey鏈接的模型,並且我希望使用django-crispy-forms將PhoneNumber模型中的字段用於我的表單。如何使用鏈接表中的字段作爲django-crispy-forms中的字段?
我試過的是使用語法'phone_number__number',但只給了我一個空的下拉列表的形式。
這裏是我的模型,用一切,但取出的客戶模型爲這個職位PHONE_NUMBER:
class Customer(models.Model):
phone_number = models.ForeignKey(PhoneNumber)
class PhoneNumber(models.Model):
TYPES = (
('Cell', 'Cell'),
('Home', 'Home'),
('Fax', 'Fax'),
('Work', 'Work'),
)
primary = models.BooleanField(default=False)
phone_type = models.CharField(max_length=30, choices=TYPES, default='Cell')
number = models.CharField(max_length=15)
我的形式,只有PHONE_NUMBER領域:
class CustomerCreateForm(forms.ModelForm):
'''
Base form for creating customers
'''
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.layout = Layout(
Fieldset(
'Personal Information',
'phone_number__number',
),
FormActions(
Submit('submit' , 'Submit' , css_class='btn btn-success'),
Button('cancel' , 'Cancel' , css_class='btn btn-warning', onclick='javascript:history.go(-1);'),
)
)
super(CustomerCreateForm, self).__init__(*args, **kwargs)
class Meta:
model = Customer
exclude = (
'create_user',
'modify_user'
)
的數字字段應該是CharField,而不是ModelChoiceField。我不是想要顯示電話號碼的一組選項,而是要給出的電話號碼的文本框。 –