我创建了两个类:Client(属性:姓名和性别)和 Account(属性:customer[fk],付款状态)。我从模板中显示表格;当我点击“添加客户”链接时,它会打开创建表单,从models.py中的内容来看,换句话说只能创建一个客户到“姓名和性别”,而不是我可以在同一个表单中集成付款情况。
模型.py
from django.db import models
class Cliente(models.Model):
SEXO = (
('M', 'Masculino'),
('F','Feminino'),
)
nome = models.CharField("Nome do cliente", max_length=255)
sexo = models.CharField("Sexo", max_length=1, choices=SEXO)
def __unicode__(self):
return self.nome
class Meta:
verbose_name = 'Cliente'
verbose_name_plural = 'Clientes'
class Conta(models.Model):
STATUS_CONTA = (
('A', 'A Pagar'),
('P', 'Pago'),
)
cliente = models.ForeignKey(Cliente, related_name='conta')
situacao = models.CharField("Situação", max_length=50, choices=STATUS_CONTA)
class Meta:
verbose_name = 'Conta'
verbose_name_plural = 'Contas'
def __unicode__(self):
return self.cliente.nome
表格.py
from django.forms.models import ModelForm
from cadastro.models import Cliente, Conta
__author__ = 'Admin'
class ClienteForm(ModelForm):
class Meta:
model = Cliente
索引.html
<html>
<body>
<a href='/adicionar_cliente/'>Adicionar cliente</a>
{% for cliente in clientes %}
{% for conta in cliente.conta.all %}
<p>Nome: {{cliente.nome}}</p>
<p>Sexo: {{cliente.get_sexo_display}} </p>
<p>Situação do pedido: {{conta.get_situacao_display}}</p>
{% endfor %}
<p><a href='/excluir/{{cliente.id}}/'>Excluir?</a></p>
<p><a href='/editar/{{cliente.id}}/'>Editar</a></p>
<hr>
{% empty %}
<h3>Não existe nenhum cliente.</h3>
{% endfor %}
</body>
</html>
表单.html
<html>
<body>
<form action='/{{action}}/' method='post'>
{% csrf_token %}
<table>
{{form}}
</table>
<input type='submit' value='Adicionar' />
</form>
</body>
</html>