I want to add a new app to my project but I do not know how to do it. I already create the templates, my views, and urls, but I do not know how to do it to be attached.
models.py (data)
from django.db import models
class Empleados(models.Model):
OPCIONES_GENERO_CHOICES = (
('M', 'Masculino'),
('F', 'Femenino'),
)
nombre = models.CharField(max_length=15)
apellidos = models.CharField(max_length=15)
ci = models.IntegerField(unique=True)
genero = models.CharField(max_length=255, choices=OPCIONES_GENERO_CHOICES, blank=True, null=True)
cargo = models.CharField(max_length=15)
creado = models.DateTimeField(auto_now_add=True)
email = models.EmailField()
telefono = models.CharField(max_length=12)
documento = models.FileField(
upload_to="archivo/",
null=True,
blank=True
)
def __str__(self):
return '%s'% (self.nombre)
class ActualizacionEmpleado(models.Model):
empleado = models.ForeignKey('Empleados', null = False, blank = False, on_delete = models.CASCADE)
fecha_actualizacion = models.DateTimeField(auto_now_add=True)
def __str__(self):
return '%s'% (self.empleado)
class DatosPersonales(models.Model):
OPCIONES_ESTADO_CIVIL_CHOICES = (
('C', 'Casado'),
('S', 'Soltero'),
('V', 'Viudo')
)
OPCIONES_GRADO_INSTRUCCION_CHOICES = (
('B', 'Bachiller'),
('U', 'Universitaria'),
('T', 'Tecnico superior')
)
empleado = models.ForeignKey('Empleados', null = False, blank = False, on_delete = models.CASCADE)
direccion = models.CharField(max_length=200)
estado_civil = models.CharField(max_length=255, choices=OPCIONES_ESTADO_CIVIL_CHOICES, blank=True, null=True)
grado_instruccion = models.CharField(max_length=255, choices=OPCIONES_GRADO_INSTRUCCION_CHOICES, blank=True, null=True)
numero_de_hijos = models.IntegerField()
def __str__(self):
return '%s'% (self.empleado)
The last class is the added one.
forms.py (personal data)
from django import forms
from django.contrib.admin import widgets
from datos.models import Empleados
from django.forms import ModelForm, TextInput, Select
class DatospersonalesForm(ModelForm):
class Meta:
model = Empleados
fields = [
'direccion',
'estado_civil',
'grado_instruccion',
'numero_de_hijos',
]
widgets = {
'direccion': TextInput(attrs={'class':'form-control','placeholder':'Introduzca su direccion'}),
'estado_civil': Select(attrs={'class':'form-control','placeholder':'Estado civil'}),
'grado_instruccion': Select(attrs={'class':'form-control','placeholder':'Grado de instruccion'}),
'numero_de_hijos': TextInput(attrs={'class':'form-control','type':'number','placeholder':'Numero de hijos'}),
}
views.py (personal data)
from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import CreateView, UpdateView, DeleteView, DetailView
from datos.models import Empleados
from datos.forms import EmpleadoForm
class DatospersonalesView(CreateView):
model = Empleados
form_class = EmpleadoForm
template_name = 'datospersonales/formulario.html'
success_url = reverse_lazy('datospersonales:index')
class DatospersonalesUpdate(UpdateView): # actualizar mi registro
model = Empleados
form_class = EmpleadoForm
template_name = 'datospersonales/formulario.html'
success_url = reverse_lazy('datospersonales:index')
class DatospersonalesDelete(DeleteView): # borrar un registro
model = Empleados
template_name = 'datospersonales/eliminar.html'
success_url = reverse_lazy('datospersonales:index')
class DetalleFormulario(DetailView): # ver detalles de un registro
model = Empleados
template_name = 'datospersonales/detalles.html'
urls.py (personal data)
from django.urls import path
from django.views.generic import ListView
from datos.views import DatospersonalesView, DatospersonalesUpdate, DatospersonalesDelete, DetalleFormulario
from .models import Empleados, ActualizacionEmpleado
app_name = 'datosempleados'
urlpatterns = [
path(
'listado',
ListView.as_view(
model = Empleados,
template_name = 'datospersonales/index.html'
),
name='listado-index'),
path('listado/formulario',
DatospersonalesView.as_view(),
name='agregar-formulario'),
path('listado/actualizar/<int:pk>/',
DatospersonalesUpdate.as_view(),
name='actualizar-formulario'),
path('listado/eliminar/<int:pk>/',
DatospersonalesDelete.as_view(),
name='eliminar-formulario'),
path('listado/detalle/<int:pk>/',
DetalleFormulario.as_view(),
name='detalle-formulario'),
]
The templates are also created.
But I do not know how to make my app personal data load, if possible from my form in the data app.
I know I'm asking too much but I'm new to this.