我的model.py中有两个表:
class Partner(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255, blank=True)
class Meta:
db_table = 'partner'
class Device(models.Model):
id = models.AutoField(primary_key=True)
partner = models.ForeignKey('Partner', null=True, blank=True)
username = models.CharField(max_length=255, blank=True)
password = models.CharField(max_length=255, blank=True)
class Meta:
db_table = 'device'
我为通用表单生成创建了一个视图:
class CreateDeviceView(CreateView):
model = Device
template_name = 'device_form.html'
#form_class = DeviceForm
def get_success_url(self):
return reverse('partners')
我从urls.py调用:
url(r'^device/new', CreateDeviceView.as_view(), name='device-new'),
模板device_form.html是:
<form action="{{ action }}" method="POST">
{% csrf_token %}
<ul>
{{ form.as_p }}
</ul>
<input type="submit" class="btn btn-info" value="Save"/>
</form>
生成的创建表单在选择菜单中有值(对于外键):Partner Object,Partner Object... 我怎样才能让选择菜单中的值成为 Partner.name?我尝试构建自己的表单,但没有得到所需的输出。