Last active
July 23, 2025 22:49
-
-
Save felipebastosweb/fe195989de7dae1a66184a4f304b82ef to your computer and use it in GitHub Desktop.
Forma de garantir que o campo DateTime irá aceitar null
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Model | |
| class Funcionario(models.Model): | |
| matricula = models.CharField(max_length=7, default="0000000") | |
| nome = models.CharField(max_length=200) | |
| # ... | |
| assuncao = models.DateField(auto_now=False, auto_now_add= False, null=True, blank=True, default= None) | |
| admissao = models.DateField(auto_now=False, auto_now_add= False, null=True, blank=True, default= None) | |
| encerramento = models.DateField(auto_now=False, auto_now_add= False, null=True, blank=True, default= None) | |
| """ | |
| # Form | |
| <div class="row"> | |
| <div class="col"> | |
| <div class="mb-3"> | |
| <label class="form-label">Data de Assunção</label> | |
| <input type="date" id="assuncao" name="assuncao" value="" class="form-control" /> | |
| </div> | |
| </div> | |
| <div class="col"> | |
| <div class="mb-3"> | |
| <label class="form-label">Data de Admissão</label> | |
| <input type="date" id="admissao" name="admissao" value="" class="form-control" /> | |
| </div> | |
| </div> | |
| <div class="col"> | |
| <div class="mb-3"> | |
| <label class="form-label">Data de Encerramento</label> | |
| <input type="date" id="encerramento" name="encerramento" value="" class="form-control" /> | |
| </div> | |
| </div> | |
| </div> | |
| """ | |
| # View | |
| Funcionario.objects.create( | |
| nome = request.POST["nome"].strip(), | |
| # ... | |
| assuncao = datetime.strptime(request.POST.get("assuncao"), "%d/%m/%Y") if request.POST.get("assuncao") else None, | |
| admissao = datetime.strptime(request.POST.get("admissao"), "%d/%m/%Y") if request.POST.get("admissao") else None, | |
| encerramento = datetime.strptime(request.POST.get("encerramento"), "%d/%m/%Y") if request.POST.get("encerramento") else None, | |
| ) | |
| def funcionarios_show(request, funcionario_id): | |
| funcionario = get_object_or_404(Funcionario, pk=funcionario_id) | |
| return render( | |
| request, | |
| "ficha_cadastral/funcionarios_show.html", | |
| { | |
| "funcionario": funcionario, | |
| } | |
| ) | |
| """ | |
| # Template | |
| <p>Data de Assunção: | |
| {% if funcionario.assuncao %} | |
| {{ funcionario.assuncao|date:"d/m/Y" }} | |
| {% else %} | |
| <em>Não informada</em> | |
| {% endif %} | |
| </p> | |
| <p>Data de Admissão: | |
| {% if funcionario.admissao %} | |
| {{ funcionario.admissao|date:"d/m/Y" }} | |
| {% else %} | |
| <em>Não informada</em> | |
| {% endif %} | |
| </p> | |
| <p>Data de Encerramento: | |
| {% if funcionario.encerramento %} | |
| {{ funcionario.encerramento|date:"d/m/Y" }} | |
| {% else %} | |
| <em>Não informada</em> | |
| {% endif %} | |
| </p> | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment