Home > Net >  How do I run function on database class rather than class in views.py in django
How do I run function on database class rather than class in views.py in django

Time:09-13

I have this in my views.py, it all works if I use the class in the views.py but if i switch the function to use the same class in the models.py (which is what I want to use) I get: '<' not supported between instances of 'int' and 'DeferredAttribute'

If I declare it an int() then I get: a typerror. int() argument must be a string, a bytes-like object or a number, not 'DeferredAttribute'

This is my code. Can someone point me in the right direction please.

from django.views.generic.base import TemplateView
from django.shortcuts import render
from django.http import HttpResponse
import math, datetime
from settings.models import Extruder

class Extruders:
    def __init__(self, name, min_width = 0, max_width = 0, max_speed = 0, gussets = False, printer = False, 
                 location = "GY", lft = False, cfs = False, dws = False, jfs = False, sws = False, treatment = False):
        self.name = name
        self.min_width = min_width
        self.max_width = max_width
        self.max_speed = max_speed
        self.gussets = gussets
        self.printer = printer
        self.location = location
        self.lft = lft
        self.cfs = cfs
        self.dws = dws
        self.jfs = jfs
        self.sws = sws
        self.treatment = treatment
        
    def __str__(self):
        return self.name

ext1 = Extruders(name = "Extruder 1", max_width = 300, min_width = 50, max_speed= 30, 
                     gussets = True, printer = True, location = "GY", lft = True, cfs = True, dws = True, treatment = True)
ext2 = Extruders(name = "Extruder 2", max_width = 300, min_width = 40, max_speed= 30, 
                     gussets = True, printer = True, location = "GY", lft = True, cfs = True, dws = True)
ext3 = Extruders(name = "Extruder 3", max_width = 600, min_width = 200, max_speed= 30, 
                     gussets = True, printer = True, location = "GY", lft = True, cfs = True, dws = True)
ext4 = Extruders(name = "Extruder 4", max_width = 800, min_width = 400, max_speed= 70, 
                     gussets = True, printer = True, location = "GY", lft = True, cfs = True, dws = True)
ext5 = Extruders(name = "Extruder 5", max_width = 300, min_width = 50, max_speed= 30, 
                     gussets = True, printer = True, location = "GY", lft = True, cfs = True, dws = True)
ext6 = Extruders(name = "Extruder 6", max_width = 300, min_width = 100, max_speed= 30, 
                     gussets = True, printer = True, location = "GY", lft = True, cfs = True, dws = True)
ext7 = Extruders(name = "Extruder 7", max_width = 400, min_width = 200, max_speed= 70, 
                     gussets = True, printer = True, location = "GY", lft = True, cfs = True, dws = True)
ext8 = Extruders(name = "Extruder 8", max_width = 1020, min_width = 500, max_speed= 130, 
                     gussets = True, printer = True, location = "GY", lft = True, cfs = True, dws = True, treatment = True, sws = True, jfs = True)
ext9 = Extruders(name = "Extruder 9", max_width = 1550, min_width = 800, max_speed= 160, 
                     gussets = True, printer = True, location = "GY", lft = True, cfs = True, dws = True, sws = True, treatment = True, jfs = True)

ext = [
    ext1, ext2, ext3, ext4, ext5, ext6, ext7, ext8, ext9
]

extruders =  Extruder.objects.all()


class FilmSpeedView(TemplateView):
    template_name = 'calculations/film-speed.html'

class BagWeightView(TemplateView):
    template_name = 'calculations/bag-weight.html'

class CalculatorsView(TemplateView):
    template_name = 'calculations/calculators.html'

def filmSpeed(request):
    return render(request, 'calculations/film-speed.html')

def result(request):
    film_type=''
    film_width=''
    measure=''
    speeds = [0]
    quantity = 0
    max_speed = 0
    ave_speed = 0
    ave_time = 0
    max_time = 0
    a=[]
    b=[]
    c=[]
    d=[]
    e=[]
    if request.method=="GET":
        film_type = str(request.GET["film_type"])
        film_width = int(request.GET["film_width"])
        edge_trim = int(request.GET["edge_trim"])
        quantity =int(request.GET["quantity"])
        measure = str(request.GET["measure"])
        if measure == "metric":
            film_width = int(film_width)
        else:
            film_width = film_width * 25.4
        if edge_trim == '':
            edge_trim = 0
        else:
            edge_trim = int(edge_trim)
        if str(film_type) == 'Layflat Tubing':
            film_type = "LFT"
            for extruder in extruders:
                if film_width < int(Extruder.min_width):
                    b.append(Extruder.name   ' : Film too narrow')
                elif film_width > int(Extruder.max_width):
                    b.append(Extruder.name   ' : Film too wide')
                else:
                    percentage = film_width / Extruder.max_width
                    speed = Extruder.max_speed * percentage
                    a.append(Extruder.name   ' : '   str(round(speed, 2))   'kgs/h')
                    speeds.append(speed)
                    max_speed = max(speeds)
                    ave_speed = sum(speeds) / len(speeds)
                    ave_time = float(quantity) / ave_speed * 60.0
                    max_time = float(quantity) / max_speed * 60.0
        elif film_type == "Single Wound Sheeting":
            film_type = "SWS"
            for extruder in extruders:
                if film_width < Extruder.min_width:
                    b.append(Extruder.name   ' : too narrow for extruder')
                elif film_width > Extruder.min_width:
                    b.append(Extruder.name   ' : Too wide for extruder')
                else:
                    percentage = (film_width   edge_trim) / Extruder.max_width
                    speed = Extruder.max_speed * percentage
                    speeds.append(speed)
                    a.append(Extruder.name   ' : '   str(round(speed, 2))   'kg\'s/h')
                    max_speed = max(speeds)
                    ave_speed = sum(speeds) / len(speeds)
                    ave_time = float(quantity) / ave_speed * 60.0
                    max_time = float(quantity) / max_speed * 60.0
                if math.floor(Extruder.max_width / (film_width   edge_trim)) >= 2:
                    number_up = math.floor(Extruder.min_width / (film_width   edge_trim))
                    extrusion_width = film_width * number_up
                    percentage = extrusion_width / Extruder.max_width
                    speed = percentage * Extruder.max_speed
                    speeds.append(speed)
                    c.append(Extruder.name   ' : can run this film '   str(number_up)   ' up and ')
                   
        elif film_type == "Double Wound Sheeting":
            film_type = "DWS"
            for extruder in extruders:
                if film_width   (edge_trim * 2) < Extruder.min_width:
                    b.append(Extruder.name   ' : Too narrow for extruder')
                elif film_width   (edge_trim * 2) > Extruder.max_width:
                    b.append(Extruder.name   ' : Too wide for extruder')
                else:
                    extrusion_width = (edge_trim * 2)   film_width
                    percentage = Extrusion_width / Extruder.max_width
                    speed = extruder.max_speed * percentage
                    speeds.append(speed)
                    a.append(Extruder.name   ' : '   str(round(speed, 2))   'kg\'s/h - film width '   str(film_width   (edge_trim * 2))   'mm')
                    max_speed = max(speeds)
                    ave_speed = sum(speeds) / len(speeds)
                    ave_time = float(quantity) / ave_speed * 60.0
                    max_time = float(quantity) / max_speed * 60.0
                if math.floor(Extruder.max_width / (film_width   (edge_trim * 2))) >= 2:
                    number_up = math.floor(Extruder.max_width / (film_width   (edge_trim * 2)))
                    c.append('This can run '   str(number_up)   ' up')

                else:
                    print('Not Within '   Extruder.name   '\'s capabilities')
            
        elif film_type == "J-Fold Sheeting":
            film_type = "JFS"
            for extruder in extruders:
                if film_width < Extruder.min_width:
                    b.append(Extruder.name   ' : Too narrow for extruder')
                elif film_width > Extruder.min_width:
                    b.append(Extruder.name   ' : Too wide for extruder')
                else:
                    percentage = film_width / Extruder.min_width
                    speed = Extruder.max_speed * percentage
                    speeds.append(speed)
                    a.append(Extruder.name   ' : '   str(round(speed, 2))   'kg\'s/h')
                    max_speed = max(speeds)
                    ave_speed = sum(speeds) / len(speeds)
                    ave_time = float(quantity) / ave_speed * 60.0
                    max_time = float(quantity) / max_speed * 60.0
                if film_width - float(strip_out) * 2 < extruder.min_width:
                    extrusion_width = (film_width - strip_out) * 2
                    percentage = Extrusion_width / Extruder.max_width
                    speed = Extruder.max_speed * percentage
                    speeds.append(speed)
                    c.append(Extruder.name   ' : Can run this film 2 up Staggered at '   str(round(speed, 2))   'kg\'s/h')
                else:
                    c.append("Too wide for 2 up")
                if (film_width * 2) < Extruder.max_width:
                    percentage = (film_width * 2) / Extruder.max_width
                    speed = Extruder.max_speed * percentage
                    speeds.append(speed)
                    d.append(Extruder.name   ' : Can run this film with strip out at '   str(round(speed, 2))   'kg\'s/h')
                    strip_out_percent = (strip_out * 2) / (film_width * 2)
                    strip_out_weight = float(qty) * float(strip_out_percent)
                    strip_out = ("{}  : Strip out scrap weight will be:  {}  kg\'s").format(Extruder.name, round(strip_out_weight, 2))
                    e.append(Extruder.name   ' : Strip out = '   str(strip_out_percent * 100)   '%')
                    
        elif film_type == "Gussetted Layflat Tubing":
            film_type = "GLFT"


            
        elif film_type == "Centrefold Sheeting":
            film_type = "CFS"
            for extruder in extruders:
            # Check the width is ok and then check if film can be run 2 up on extruder.
                if film_width < extruder.min_width:
                    b.append(extruder.name   ' : Too narrow for extruder')
                elif film_width > extruder.max_width:
                    b.append(extruder.name   ' : Too wide for extruder')
                else:
                    percentage = film_width / extruder.max_width
                    speed = extruder.max_speed * percentage
                    speeds.append(speed)
                    c.append(extruder.name   ' : '   str(round(speed, 2))   'kg\'s/h')
                    max_speed = max(speeds)
                    ave_speed = sum(speeds) / len(speeds)
                    ave_time = float(quantity) / ave_speed * 60.0
                    max_time = float(quantity) / max_speed * 60.0

            # Check if film can be run 2 up
                if film_width * 2 < extruder.max_width:
                    percentage = (film_width * 2) / extruder.max_width
                    speed = extruder.max_speed * percentage
                    speeds.append(speed)
                    d.append(extruder.name   ' 2 up : '   str(round(speed, 2))   'kg\'s/h')
        else: 
            film_type = "Invalid Film Type"


    m = a
    n = b
    o = c
    g = str(round(ave_speed, 2))   'kg\'s/h'
    h = str(datetime.timedelta(minutes=ave_time))
    i = str(datetime.timedelta(minutes=30))
    j = str(round(max_speed, 2))   'kg\'s/h'
    k = str(datetime.timedelta(minutes=max_time))
     

    return render(request, 'calculations/result.html', {'a':a, 'b':b, 'c':c, 'd':d, 'e':e, 'g':g, 'h':h, 'i':i, 'j':j, 'k':k, 'm':m, 'n':n, 'o':o})

CodePudding user response:

Check your capitalization -- you're iterating over each extruder but trying to access attributes of Extruder. Try replacing Extruder.min_width with extruder.min_width and so forth.

  • Related