7
If I have in mine model a class usuario, a phones and a class manager, who are one to one, so:
class Usuario(models.Model):
login = models.CharField(max_length=50, unique=True)
senha = models.CharField(max_length=50)
telefone = models.ForeignKey(TelefonesUsuario)
class TelefonesUsuario(models.Model):
telefone = models.CharField(max_length=11)
class Gerente(models.Model):
departamento = models.CharField(max_length=10)
usuario = models.OneToOneField(Usuario)
If I want to register a new client, how do I? (abstracting layout)
I tried that so far:
In the Forms:
from django import forms
from app.models import Usuario
class FormUsuario(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput())
class Meta:
model = Usuario
In view:
def teste(request):
form = FormUsuario()
return render_to_response("teste.html", {'form': form}, context_instance=RequestContext(request))
And in . html:
<form action="" method="post">
{% csrf_token %}
{{ form.login }}
{{ form.senha }}
{# {{ form.telefone }} -> exception #}
{{ form.departamento }} {# -> não aparece na página #}
</form>
Can someone give me a hint of how I follow?
If you are learning, I recommend starting with Django 1.8 at once. In this case, the
{{ form.departamento }}will not appear because it is part of the modelGerente. What you can do is add a fielddepartamentoin theFormUsuario, and after saving the form create a manager with the chosen department and user.– Paulo
Hi. Yes I’ll give myself an update on Django 1.8, I really need it! As for Phone, in Asp.Net I would normally make a grid within the form to add dynamically. Already department I would enter normally and code Behind would assign in the related field. The hint of the field in the form seems good, but I still don’t know how to have all the insertion in one screen, so I don’t know how to apply it this way.
– Vinícius Bastos
If you want to add multiple phones you will have to use
manytomanyfieldand notforeignkey. And thisgridwould in case aformsetin Django, where your view would have two Foms. See Django’s documentation onformsets– Paulo