import multiple models in another Django 2.X model

0

I am trying to create a form for my project with Django 2.0, with Python 3.6.4 in Windows 10, but when using a reference of some models of another app it tells me that it does not exist or that it is not defined, in a similar question said that a reference to the beginning using models was necessary. (models.Employee) but if I do this it tells me the following error:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x05F7F0C0>
Traceback (most recent call last):
  File "C:\Users\FERNAN~1\Envs\MYPROJ~1\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\FERNAN~1\Envs\MYPROJ~1\lib\site-packages\django\core\management\commands\runserver.py", line 113, in inner_run
    autoreload.raise_last_exception()
  File "C:\Users\FERNAN~1\Envs\MYPROJ~1\lib\site-packages\django\utils\autoreload.py", line 248, in raise_last_exception
    raise _exception[1]
  File "C:\Users\FERNAN~1\Envs\MYPROJ~1\lib\site-packages\django\core\management\__init__.py", line 327, in execute
    autoreload.check_errors(django.setup)()
  File "C:\Users\FERNAN~1\Envs\MYPROJ~1\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\FERNAN~1\Envs\MYPROJ~1\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\FERNAN~1\Envs\MYPROJ~1\lib\site-packages\django\apps\registry.py", line 112, in populate
    app_config.import_models()
  File "C:\Users\FERNAN~1\Envs\MYPROJ~1\lib\site-packages\django\apps\config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Users\FERNAN~1\Envs\MYPROJ~1\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\Fernando Leon\dev\django\oasiSystem\apps\captura\models.py", line 12, in <module>
    class Turno(models.Model):
  File "C:\Users\Fernando Leon\dev\django\oasiSystem\apps\captura\models.py", line 16, in Turno
    no_parte = models.ForeignKey(models.NumParte, null=True, blank=True, on_delete=models.CASCADE)
AttributeError: module 'django.db.models' has no attribute 'NumParte'

and if I remove that reference the error that it tells me is the following:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x068DF0C0>
Traceback (most recent call last):
  File "C:\Users\FERNAN~1\Envs\MYPROJ~1\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\FERNAN~1\Envs\MYPROJ~1\lib\site-packages\django\core\management\commands\runserver.py", line 113, in inner_run
    autoreload.raise_last_exception()
  File "C:\Users\FERNAN~1\Envs\MYPROJ~1\lib\site-packages\django\utils\autoreload.py", line 248, in raise_last_exception
    raise _exception[1]
  File "C:\Users\FERNAN~1\Envs\MYPROJ~1\lib\site-packages\django\core\management\__init__.py", line 327, in execute
    autoreload.check_errors(django.setup)()
  File "C:\Users\FERNAN~1\Envs\MYPROJ~1\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\FERNAN~1\Envs\MYPROJ~1\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\FERNAN~1\Envs\MYPROJ~1\lib\site-packages\django\apps\registry.py", line 112, in populate
    app_config.import_models()
  File "C:\Users\FERNAN~1\Envs\MYPROJ~1\lib\site-packages\django\apps\config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Users\FERNAN~1\Envs\MYPROJ~1\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\Fernando Leon\dev\django\oasiSystem\apps\captura\models.py", line 12, in <module>
    class Turno(models.Model):
  File "C:\Users\Fernando Leon\dev\django\oasiSystem\apps\captura\models.py", line 16, in Turno
    no_parte = models.ForeignKey(NumParte, null=True, blank=True, on_delete=models.CASCADE)
NameError: name 'NumParte' is not defined

my model.py code is as follows:

from django.db import models
from django.forms import ModelForm

from .models import *

# Create your models here.
class TipoTurno(models.Model):
    nombre = models.CharField(max_length=5)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)

class Turno(models.Model):
    comentario = models.CharField(max_length=140, null=True)
    turno = models.ForeignKey(TipoTurno, null=True, blank=True, on_delete=models.CASCADE)
    fecha_realizado = models.DateField(null=True)
    no_parte = models.ForeignKey(NumParte, null=True, blank=True, on_delete=models.CASCADE)
    inspector = models.ForeignKey(Empleado, null=True, blank=True, on_delete=models.CASCADE)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)

class Asistencia(models.Model):
    empleado = models.ForeignKey(Empleado, null=True, blank=True, on_delete=models.CASCADE)
    comentarios = models.CharField(max_length=140, null=True)
    fecha_inicio = models.DateField()
    fecha_fin = models.DateField()
    hora_inicio = models.DateTimeField(auto_now=False, auto_now_add=False)
    hora_fin = models.DateTimeField(auto_now=False, auto_now_add=False)
    total_hrs_trabajadas = models.DateTimeField(auto_now=False, auto_now_add=False)
    turno = models.ForeignKey(Turno, null=True, blank=True, on_delete=models.CASCADE)
    most_analisis = models.ForeignKey(MostrarAnalisis, null=True, blank=True, on_delete=models.CASCADE)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)

class TipoValor(models.Model):
    nombre = models.CharField(max_length=10)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)

class Campo(models.Model):
    # Falta anotar lo de la tabla de piece_items
    nombre = models.CharField(max_length=25)
    descripcion = models.CharField(max_length=140)
    tipo = models.ForeignKey(TipoValor, null=True, blank=True, on_delete=models.CASCADE)
    requerido = models.ForeignKey(Requerido, null=True, blank=True, on_delete=models.CASCADE)
    estatus = models.ForeignKey(Estatus, null=True, blank=True, on_delete=models.CASCADE)
    editable = models.ForeignKey(EsEditable, null=True, blank=True, on_delete=models.CASCADE)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)

class CapturaCampo(models.Model):
    valor = models.CharField(max_length=140)
    campo = models.ForeignKey(Campo, null=True, blank=True, on_delete=models.CASCADE)
    fecha = models.DateField(null=False)
    captura = models.ForeignKey(Captura, null=True, blank=True, on_delete=models.CASCADE)
    most_analisis = models.ForeignKey(MostrarAnalisis, null=True, blank=True, on_delete=models.CASCADE) 
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)

class Defecto(models.Model):
    nombre = models.CharField(max_length=25)
    descripcion = models.CharField(max_length=140)
    estatus = models.ForeignKey(Estatus, null=True, blank=True, on_delete=models.CASCADE)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)

class CapturaDefecto(models.Model):
    nombre = models.CharField(max_length=140)
    valor = models.IntegerField()
    defecto = models.ForeignKey(Defecto, null=True, blank=True, on_delete=models.CASCADE)
    fecha = models.DateField()
    captura = models.ForeignKey(Captura, null=True, blank=True, on_delete=models.CASCADE)
    most_analisis = models.ForeignKey(MostrarAnalisis, null=True, blank=True, on_delete=models.CASCADE)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)

class Captura (models.Model):
    cotizacion = models.ForeignKey(Cotizacion, null=True, blank=True, on_delete=models.CASCADE)
    fecha = models.DateField()
    turno = models.ForeignKey(Turno, null=True, blank=True, on_delete=models.CASCADE)
    usuario = models.ForeignKey(Usuario, null=True, blank=True, on_delete=models.CASCADE)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)

class TipoTurnoForm(ModelForm):
    class Meta:
        model = TipoTurno
        fields = ('nombre',)

class TurnoForm(ModelForm):
    class Meta:
        model = Turno
        fields = (
            'comentario',
            'turno',
            'fecha_realizado',
            'no_parte',
            'inspector',
        )

class AsistenciaForm(ModelForm):
    class Meta:
        model = Asistencia
        fields = (
            'empleado',
            'comentarios',
            'fecha_inicio',
            'fecha_fin',
            'hora_inicio',
            'hora_fin',
            'total_hrs_trabajadas',
            'turno',
            'most_analisis',
        )

class TipoValorForm(ModelForm):
    class Meta:
        model = TipoValor
        fields = ('nomrbre',)

class CampoForm(ModelForm):
    class Meta:
        model = Campo
        fields = (
            'nombre',
            'descripcion',
            'tipo',
            'requerido',
            'estatus',
            'editable',
        )

class CapturaCampoForm(ModelForm):
    class Meta:
        model = CapturaCampo
        fields = (
            'valor',
            'campo',
            'fecha',
            'captura',
            'most_analisis',
        )

class DefectoForm(ModelForm):
    class Meta:
        model = Defecto
        fields = (
            'nombre',
            'descripcion',
            'estatus',
        )

class CapturaDefectoForm(ModelForm):
    class Meta:
        model = CapturaDefecto
        fields = (
            'nombre',
            'valor',
            'defecto',
            'fecha',
            'captura',
            'most_analisis',
        )

class CapturaForm(ModelForm):
    class Meta:
        model = Captura
        fields = (
            'cotizacion',
            'fecha',
            'turno',
            'usuario',
        )

the code where I have added the model 'NumParte' is the following:

from django.db import models
from django.forms import ModelForm
from .models import * 

# Create your models here.
class Visible(models.Model):
    nombre = models.CharField(max_length=10)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)

    def __str__(self):
        return self.nombre

class Estatus(models.Model):
    nombre = models.CharField(max_length=15)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)

    def __str__(self):
        return self.nombre

class EsEditable(models.Model):
    nombre = models.CharField(max_length=15)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)
    def __str__(self):
        return self.nombre

class Requerido(models.Model):
    nombre = models.CharField(max_length=15)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)
    def __str__(self):
        return self.nombre

class MostrarAnalisis(models.Model):
    nombre = models.CharField(max_length=15)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)
    def __str__(self):
        return self.nombre

class MostrarCotizacion(models.Model):
    nombre = models.CharField(max_length=15)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)
    def __str__(self):
        return self.nombre

class Archivada(models.Model):
    nombre = models.CharField(max_length=15)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)
    def __str__(self):
        return self.nombre

class NumParte(models.Model):
    nombre = models.CharField(max_length=80)
    cont = models.IntegerField(blank=True, null=True)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)
    def __str__(self):
        return self.nombre

class TipoRate(models.Model):
    nombre = models.CharField(max_length=25)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)
    def __str__(self):
        return self.nombre

class Idioma(models.Model):
    nombre = models.CharField(max_length=15)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)
    def __str__(self):
        return self.nombre

class Moneda(models.Model):
    nombre = models.CharField(max_length=15)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)
    def __str__(self):
        return self.nombre

class Impuesto(models.Model):
    nombre = models.CharField(max_length=15)
    valor = models.DecimalField(null=False,max_digits=2,decimal_places=2)
    #procedencia = models.ForeignKey
    activo = models.ForeignKey(Estatus, null=True, blank=True, on_delete=models.CASCADE)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)
    def __str__(self):
        return self.nombre

class TipoOperacion(models.Model):
    nombre = models.CharField(max_length=15)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)
    def __str__(self):
        return self.nombre

class Correo(models.Model):
    # persona = models.ManyToManyField(Empleado, db_table="contact-mail")
    correo = models.CharField(max_length=120) 
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)
    def __str__(self):
        return self.correo

class Telefono(models.Model):
    # persona = models.ManyToManyField(Empleado, db_table="contact-tel")
    telefono = models.IntegerField()
    extencion = models.IntegerField(default="0", null=True)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)
    def get_fullphone():
        return self.telefono + ' ext. ' + self.extencion

    def __str__(self):
        return get_fullphone

class ConceptoDeCobro(models.Model):
    nombre = models.CharField(max_length=20)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)
    def __str__(self):
        return self.nombre

class TipoDeTrabajo(models.Model):
    nombre = models.CharField(max_length=20)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)
    def __str__(self):
        return self.nombre

class TipoMoneda(models.Model):
    nombre = models.CharField(max_length=25)
    nombre_corto = models.CharField(max_length=3)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)
    def __str__(self):
        return self.nombre

class TipoTurno(models.Model):
    nombre = models.CharField(max_length=5)
    horario = models.CharField(max_length=45, null=True)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)
    def get_fullname():
        return self.nombre + ' ' + self.horario

    def __str__(self):
        return get_fullname()

class Parametro(models.Model):
    nombre = models.CharField(max_length=512)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)

class DetalleTrabajo(models.Model):
    detalle = models.CharField(max_length=140)
    created_at = models.DateField(auto_now_add=True, null=True)
    updated_at = models.DateField(auto_now=True)
    def __str__(self):
        self.detalle

class VisibleForm(ModelForm):
    class Meta:
        model = Visible
        fields = ('nombre',)

class EstatusForm(Modelform):
    class Meta:
        model = Estatus
        fields = ('nombre',)

class EsEditableForm(ModelForm):
    class Meta:
        model = EsEditable
        fields = ('nombre',)

class RequeridoForm(Modelform):
    class Meta:
        model = Requerido
        fields = ('nombre',)

class MostrarAnalisisForm(ModelForm):
    class Meta:
        model = MostrarAnalisis
        fiels = ('nombre',)

class MostrarCotizacionForm(ModelForm):
    class Meta:
        model = MostrarCotizacion
        fields = ('nombre',)

class ArchivadaForm(ModelForm):
    class Meta:
        model = Archivada
        fields = ('Archivada',)

class NumParteForm(ModelForm):
    class Meta:
        model = NumParte
        fields = ('nombre',)

class TipoRateForm(ModelForm):
    class Meta:
        model = TipoRate
        fields = ('nombre',)

class IdiomaForm(ModelForm):
    class Meta:
        model = Idioma
        fields = ('nombre',)

class MonedaForm(ModelForm):
    class Meta:
        model = Moneda
        fields = ('nombre',)

class ImpuestoForm(ModelForm):
    class Meta:
        model = Impuesto
        fields = ('nombre',)

class TipoOperacionForm(ModelForm):
    class Meta:
        model = TipoOperacion
        fields = ('nombre',)

class Correoform(ModelForm):
    class Meta:
        model = Correo
        fields = ('nombre',)

class TelefonoForm(ModelForm):
    class Meta:
        model = Telefono
        fields = ('telefono', 'extension',)

class ConceptoDeCobroForm(ModelForm):
    class Meta:
        model = ConceptoDeCobro
        fields = ('nombre')

class TipoMonedaForm(ModelForm):
    class Meta:
        model = TipoMoneda
        fields = ('nombre',)

class TipoDeTrabajoForm(ModelForm):
    class Meta:
        model = TipoDeTrabajo
        fields = ('nombre',)

class TipoTurnoForm(ModelForm):
    class Meta:
        model = TipoTurno
        fields = ('nombre', 'horario',)

class DetalleTrabajoForm(ModelForm):
    class Meta:
        model = DetalleTrabajo
        fields = ('nombre',)

class ParametroForm(ModelForm):
    class Meta:
        model = Parametro
        Fields = ('nombre',)

I hope someone can help me, I would really appreciate it, I hope you can see my mistake that I can not detect. Thanks in advance

    
asked by Fernando A. León 23.04.2018 в 21:15
source

1 answer

0

When importing a model or something else by . you mean that this file is local within the same app, I explain myself better. If a Django project is made up of 2 or more example apps: RecusosHumanos, Inventory and once inside the views.py that belongs to RecursosHumanos you want to import all the models of himself, you can declare

from .models import * 

In this way you import all the models belonging to the App that are found in models.py in your own Directory. The other thing is that you need in the Human Resources views.py to use one or several models of the Inventory App, then if you should verify that in your global settings.py in INSTALLED_APPS is the App that you need to use, that is the first step example:

INSTALLED_APPS = [
    'recusroshumanos',
    'inventario',
    'django.contrib.admin'
] 

Once defined you can, anywhere in the Human Resources Resources App import what you need from the Inventory App. example:

RecusosHumanos / views.py

from inventario.models import *

In this way you have access from RecusrosHumanos to all the models defined in the models.py within the App Inventory. It should also be noted that this philosophy is not only applicable in the case of models, you can use it to import what you need to reuse

    
answered by 24.04.2018 / 17:29
source