Katsuya Obara
Katsuya Obara

Reputation: 963

Cannot find which model is getting error in Django

I have following model and after Migrate, I tried to add data through Admin page.
However, I got the error NOT NULL constraint failed: HVAC_chiller.basicinfo_ptr_id.
Referring to other stackoverflow, I understood problem. However, I cannot find any model which has attribute ptr. Does anyone know which attribute I should revise?

models.py

class BasicInfo(models.Model):
    title = models.CharField(max_length=100)
    manufacturer = models.CharField(max_length=50, blank = True)
    used_project = models.CharField(max_length=50, null=True,blank=True)
    cost=models.IntegerField(null = True, blank = True)
    url=models.URLField(null=True,blank=True)
    comment=models.TextField(null=True,blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

class VRV(BasicInfo):
    InletT = models.FloatField(blank=True)
    OutletT = models.FloatField(blank=True)
    Capacity = models.IntegerField(blank=True)  # Reference capacity
    COP = models.FloatField(blank=True)  # Reference COP

class CapacityFunction(models.Model):
    name=models.CharField(max_length=100, blank = True)
    c1=models.FloatField()
    c2=models.FloatField()
    c3 = models.FloatField()
    c4 = models.FloatField()
    c5 = models.FloatField()
    c6 = models.FloatField()
    minX= models.FloatField(blank=True)
    maxX = models.FloatField(blank=True)
    minY = models.FloatField(blank=True)
    maxY = models.FloatField(blank=True)

    def __str__(self):
        return self.name

class EIRofTemp(CapacityFunction):
    pass

class EIRofPLR(models.Model):
    name = models.CharField(max_length=100, blank=True)
    c1=models.FloatField()
    c2= models.FloatField()
    c3 = models.FloatField()
    c4 = models.FloatField(blank=True,null=True)
    minX = models.FloatField(blank=True)
    maxX = models.FloatField(blank=True)

    def __str__(self):
        return self.name

class Chiller(VRV):
    CONDENSER_CHOICES = (
        ('WaterCooled','WaterCooled'),
        ('AirCooled', 'AirCooled'),
        ('EvaporativelyCooled','EvaporativelyCooled')
    )
    Condenser=models.CharField(max_length=15,choices=CONDENSER_CHOICES,default='Water')
    CHWFlowRate=models.FloatField(blank=True,null=True)
    CWFlowRate=models.FloatField(blank=True,null=True)
    minPLR=models.FloatField(blank=True,null=True)
    maxPLR=models.FloatField(blank=True,null=True)
    optimumPLR=models.FloatField(blank=True,null=True)
    minUnloadRatio=models.FloatField(blank=True,null=True)
    CapacityFunction=models.OneToOneField(CapacityFunction,blank=True,null=True,on_delete=models.CASCADE,related_name='cap')
    EIRofTemp=models.OneToOneField(EIRofTemp,blank=True,null=True,on_delete=models.CASCADE,related_name='eirtemp')
    EIRofPLR = models.OneToOneField(EIRofPLR, blank=True,null=True, on_delete=models.CASCADE,related_name='eirplr')

Upvotes: 0

Views: 82

Answers (1)

Dhaval Savalia
Dhaval Savalia

Reputation: 525

You have tried multi-inheritance. Try to use ForeignKey()

class BasicInfo(models.Model):
    title = models.CharField(max_length=100)
    manufacturer = models.CharField(max_length=50, blank = True)
    used_project = models.CharField(max_length=50, null=True,blank=True)
    cost=models.IntegerField(null = True, blank = True)
    url=models.URLField(null=True,blank=True)
    comment=models.TextField(null=True,blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

class VRV(models.Model):
    basic_info = models.ForeignKey(BasicInfo) # Foreign Key on BasicInfo model
    InletT = models.FloatField(blank=True)
    OutletT = models.FloatField(blank=True)
    Capacity = models.IntegerField(blank=True)
    COP = models.FloatField(blank=True)

class Chiller(models.Model):
    CONDENSER_CHOICES = (
        ('WaterCooled','WaterCooled'),
        ('AirCooled', 'AirCooled'),
        ('EvaporativelyCooled','EvaporativelyCooled'))
    vrv = models.ForeignKey(VRV) # Foreign Key of VRV model
    Condenser=models.CharField(max_length=15, choices=CONDENSER_CHOICES, default='Water')
    CHWFlowRate=models.FloatField(blank=True,null=True)
    CWFlowRate=models.FloatField(blank=True,null=True)
    minPLR=models.FloatField(blank=True,null=True)
    maxPLR=models.FloatField(blank=True,null=True)
    optimumPLR=models.FloatField(blank=True,null=True)
    minUnloadRatio=models.FloatField(blank=True,null=True)
    CapacityFunction=models.OneToOneField(CapacityFunction,blank=True,null=True,on_delete=models.CASCADE,related_name='cap')
    EIRofTemp=models.OneToOneField(EIRofTemp,blank=True,null=True,on_delete=models.CASCADE,related_name='eirtemp')
    EIRofPLR = models.OneToOneField(EIRofPLR, blank=True,null=True, on_delete=models.CASCADE,related_name='eirplr')

Try to use standard Django/Python syntax conventions. for example,

  1. Use lowercase for defining a variable.
  2. Define CHOICES outside of class.

Upvotes: 1

Related Questions