-1
With the primary key ID, work with the information in the views, but it does not appear in the edit(html), the fields sector and company appears.
Views
@login_required(login_url='/login/')
def ed_ramal(request):
id_ramal = request.GET.get('id')
id_setor = Ramais.objects.filter(id=id_ramal).values('setor_ramais')
id_empresa = Ramais.objects.filter(id=id_ramal).values('empresa_ramais')
if id_ramal:
ed_ramal = Ramais.objects.filter(id=id_ramal).values('ramal')
i_resp = Ramais.objects.filter(id=id_ramal).values('nome_resp')
i_mail = Ramais.objects.filter(id=id_ramal).values('email')
setor = Setores.objects.filter(id=id_setor[0]['setor_ramais'])
empresa= Empresas.objects.filter(id=id_empresa[0]['empresa_ramais'])
dados = {'ramal': ed_ramal, 'responsavel': i_resp, 'email': i_mail, 'setores': setor, 'empresas': empresa}
else:
empresa = Empresas.objects.all()
setor = Setores.objects.all()
dados = {'empresas': empresa, 'setores': setor}
return render(request, 'ramal.html', dados)
HTML that is called to do the editing, I am passing the id(primary key of the extension )
<section class="container w-auto bgcolor_cad">
<form action="submit" method="POST">{% csrf_token %}
<input type="number" name="id_ramal" value="{{ i.id }}" hidden>
<div class="form-row">
<div class="form-group col-md-4">
<label>Ramal:</label>
<input type="number" name="ramal" class="form-control" value="{{ i.ramal }}" >
</div>
<div class=" col-md-4">
<label>Responsável:</label>
<input type="text" name="responsavel" class="form-control" value="{{ i.nome_resp }}" >
</div>
</div>
<div class="form-row">
<div class="col-md-4">
<label>Email:</label>
<input type="email" name="email" class="form-control" value="{{ i.email }}">
</div>
<div class="col-md-4">
<label >Setor:</label>
<select id="inputState" name="setor" class="form-control">
{% for s in setores %}
<option value="{{ s.id }}" name="pk_setor">{{ s.setor }}</option>
{% endfor %}
</select>
<a href="/ramais/edicao/setor/">
<button type="button" class="btn btn-info">ADICIONAR</button>
</a>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<label for="inputState">Empresa:</label>
<select id="inputState01" name="empresa" class="form-control">
{% for e in empresas %}
<option value="{{ e.id }}" name="pk_empresa">{{ e.nome_emp }}</option>
{% endfor %}
</select>
</div>
</div>
<button type="submit" class="btn btn-success">SALVAR</button>
<a href="/ramais/">
<button type="button" class="btn btn-danger">CANCELAR</button>
</a>
</form>
Models
class Setores(models.Model):
setor = models.CharField(max_length=150, unique=True)
class Meta:
db_table = 'setores'
def __str__(self):
return self.setor
class Empresas(models.Model):
nome_emp = models.CharField(max_length=250,verbose_name='Empresa')
cnpj = models.CharField(max_length=21, verbose_name='CNPJ')
insc_estadual= models.CharField(max_length=13, blank=True, verbose_name='Inscrição Estadual')
telefone_emp = models.CharField(max_length=13, verbose_name='Telefone')
rua = models.CharField(max_length=500, verbose_name='Endereço')
numero = models.CharField(max_length=6, verbose_name='Número')
complemento = models.CharField(max_length=150, blank=True, verbose_name='Complemento')
bairro = models.CharField(max_length=150, verbose_name='Bairro')
cep = models.CharField(max_length=9, verbose_name='CEP')
class Meta:
db_table = 'empresas'
def __str__(self):
return self.nome_emp
class Ramais(models.Model):
ramal = models.CharField(max_length=4, verbose_name='Ramal')
nome_resp = models.CharField(max_length=30, verbose_name='Responsavél')
email = models.EmailField(max_length=60)
setor_ramais = models.ForeignKey(Setores, on_delete= models.DO_NOTHING, verbose_name='Setor')
empresa_ramais = models.ForeignKey(Empresas,on_delete= models.DO_NOTHING, verbose_name='Empresa')
data_criacao = models.DateTimeField(auto_now=True)
class Meta:
db_table = 'ramais'
def __str__(self):
return self.ramal + str(self.setor_ramais)+ str(self.empresa_ramais)
def get_data_criacao(self):
return self.data_criacao.strftime('%d/%m/%Y')
Thanks, it’s working. I don’t know how to use this query in db, I know how to do it through the tools. And in the case of i.ramais in the template, I didn’t know how to send the data and I thought it would be "i.ramais" as a "for", but I understood that you put "extensions.id" would be "extensions" of the database, right?
– Cristian Camargo
Hi, when going through the context the object 'extensions', it has several attributes and one of them the id, to access this via template just that, extensions.id.
– Ernesto Casanova
thank you very much. It helped me a lot and this is my first system. I will not miss but these details.
– Cristian Camargo