我已经定义了 UerProfile 并且需要为 MyForm 获取电话数据
class UserProfile(models.Model):
...
phone = models.CharField(max_length=20, blank=True)
class CustomModel(models.Model):
...
phone = models.CharField(max_length=20)
class MyForm(forms.ModelForm):
class Meta:
model = CustomModel
exclude = ['some_fields_but_not_phone',]
现在,如果已设置,我需要从 UserProfile 中获取电话数据,以便在 {{ form.phone }} 字段中预先填充值。然后用户可以在提交表单时更改或保持原样。