help with Django Models

1

I am new in the programming field and I am learning django and python at the same time. I want to create a database with three tables. project, parts and processes. A project can have several pieces, but a piece can not belong to several projects. The part table can have several processes.

in my models.py create the following classes

from django.db import models


# Create your models here.
class Proyecto(models.Model):
    PO = models.CharField(max_length=70)
    estado = models.CharField(max_length=10)
    fechaInicio = models.DateTimeField()
    fechaTerminacion = models.DateTimeField()
    observaciones = models.TextField(max_length=500)


class Pieza(models.Model):
    PO = models.ForeignKey(Proyecto, on_delete=models.CASCADE)
    noPlano = models.CharField(max_length=70)
    cantidad = models.IntegerField()
    responsable = models.CharField(max_length=70)
    procesos = models.ForeignKey(Procesos, on_delete=models.CASCADE)


class Procesos(models.Model):
    noPlano = models.ForeignKey(Pieza, on_delete=models.CASCADE)
    corte = models.BooleanField(default=False)
    escuadre = models.BooleanField(default=False)
    maquinado = models.BooleanField(default=False)
    rectificado = models.BooleanField(default=False)
    pulido = models.BooleanField(default=False)

and I have the following problems in the "Parts" class I find an error with the field "processes"

procesos = models.ForeignKey(Procesos, on_delete=models.CASCADE)
NameError: name 'Procesos' is not defined

additional question: how can I make the process table available for the parts table? the idea is that in my template I can display a list of pieces belonging to the same project and that shows the processes that each piece has.

    
asked by victorR 19.12.2016 в 23:11
source

1 answer

1

It gives you an error because in Pieces when you refer to Processes , this last class has not yet been declared, because it is later.

Between Parts and Processes you have a Many to Many relationship if I am not mistaken. Well, you can not have a ForeignKey in Processes to Parts and another ForeignKey in Parts to processes. That in Django would be solved like this:

class Pieza(models.Model):
    PO = models.ForeignKey(Proyecto, on_delete=models.CASCADE)
    noPlano = models.CharField(max_length=70)
    cantidad = models.IntegerField()
    responsable = models.CharField(max_length=70)

class Procesos(models.Model):
    noPlano = models.ManyToManyField(Pieza, on_delete=models.CASCADE)
    corte = models.BooleanField(default=False)
    escuadre = models.BooleanField(default=False)
    maquinado = models.BooleanField(default=False)
    rectificado = models.BooleanField(default=False

This way noPlan is a Parts list.

    
answered by 19.12.2016 / 23:29
source