Nameerror on Django, but the class ta defined

Asked

Viewed 97 times

1

When rotating the python3 manage.py runsserver i get the bug :

Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "/home/savio/.local/lib/python3.6/site-packages/django/utils/autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "/home/savio/.local/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "/home/savio/.local/lib/python3.6/site-packages/django/utils/autoreload.py", line 77, in raise_last_exception
    raise _exception[1]
  File "/home/savio/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 337, in execute
    autoreload.check_errors(django.setup)()
  File "/home/savio/.local/lib/python3.6/site-packages/django/utils/autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "/home/savio/.local/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/savio/.local/lib/python3.6/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/home/savio/.local/lib/python3.6/site-packages/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python3.6/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 "/home/savio/Downloads/proGEN-master/progen/apps/cargas/models/__init__.py", line 3, in <module>
    from .romaneio import *
  File "/home/savio/Downloads/proGEN-master/progen/apps/cargas/models/romaneio.py", line 22, in <module>
    class VisualizarRomaneio(Carga):
NameError: name 'Carga' is not defined

But the name Load is set, and it does not release:

# -*- coding: utf-8 -*-

from django.db import models
from django.template.defaultfilters import date
from django.core.validators import MinValueValidator
from django.urls import reverse_lazy

from decimal import Decimal

from progen.apps.fiscal.models import PIS, COFINS
from progen.apps.estoque.models import DEFAULT_LOCAL_ID

import locale
locale.setlocale(locale.LC_ALL, '')

STATUS_CARGA_ESCOLHAS = (
    (u'0', u'Aberto'),
    (u'1', u'Faturado'),
    (u'2', u'Cancelado'),
)

class VisualizarRomaneio(Carga):
    carga = models.ForeignKey(
        'vendas.OrcamentoVenda', related_name="orcamento_pedido",
        on_delete=models.SET_NULL, null=True, blank=True)
    data_entrega = models.DateField(null=True, blank=True)
    status = models.CharField(
        max_length=1, choices=STATUS_CARGA_ESCOLHAS, default='0')

    class Meta:
        verbose_name = "Romaneio"
        permissions = (
            ("ver_romaneio", "Pode visualizar romaneios"),
        )

    @property
    def format_data_entrega(self):
        return '%s' % date(self.data_entrega, "d/m/Y")

    @property
    def tipo_romaneio(self):
        return 'Romaneio'

    def edit_url(self):
        return reverse_lazy('vendas:editarpedidovendaview', kwargs={'pk': self.id})

    def __unicode__(self):
        s = u'Romaneio nº %s (%s)' % (
            self.id, self.get_status_display())
        return s

    def __str__(self):
        s = u'Romaneio nº %s (%s)' % (
            self.id, self.get_status_display())
        return s


class Carga(models.Model):
   # Pedido
    pedido = models.ForeignKey(
        'vendas.Venda', related_name="venda_pedido", on_delete=models.CASCADE)
    ind_final = models.BooleanField(default=True)
    # Transporte
    transportadora = models.ForeignKey(
        'cadastro.Transportadora', related_name="venda_transportadora", on_delete=models.CASCADE, null=True, blank=True)
    veiculo = models.ForeignKey('cadastro.Veiculo', related_name="venda_veiculo",
                                on_delete=models.SET_NULL, null=True, blank=True)
    # Info
    data_emissao = models.DateField(null=True, blank=True)
    vendedor = models.CharField(max_length=255, null=True, blank=True)
    valor_total = models.DecimalField(max_digits=13, decimal_places=2, validators=[
                                      MinValueValidator(Decimal('0.00'))], default=Decimal('0.00'))

    @property
    def format_data_emissao(self):
        return '%s' % date(self.data_emissao, "d/m/Y")

    def get_valor_total_attr(self, nome_attr):
        valor_total = 0
        for item in self.itens_venda.all():
            v = getattr(item, nome_attr, 0)
            if v:
                valor_total += v

        return valor_total

    def __unicode__(self):
        s = u'Romaneio nº %s' % (self.id)
        return s

    def __str__(self):
        s = u'Romaneio nº %s' % (self.id)
        return s

1 answer

0

The class Carga is defined in your code after the class VisualizarRomaneio(class that inherits Carga). In Python you can only inherit classes created before. To fix this problem, type the class Carga before VisualizarRomaneio, thus:

# -*- coding: utf-8 -*-

from django.db import models
from django.template.defaultfilters import date
from django.core.validators import MinValueValidator
from django.urls import reverse_lazy

from decimal import Decimal

from progen.apps.fiscal.models import PIS, COFINS
from progen.apps.estoque.models import DEFAULT_LOCAL_ID

import locale
locale.setlocale(locale.LC_ALL, '')

STATUS_CARGA_ESCOLHAS = (
    (u'0', u'Aberto'),
    (u'1', u'Faturado'),
    (u'2', u'Cancelado'),
)

class Carga(models.Model):
   # Pedido
    pedido = models.ForeignKey(
        'vendas.Venda', related_name="venda_pedido", on_delete=models.CASCADE)
    ind_final = models.BooleanField(default=True)
    # Transporte
    transportadora = models.ForeignKey(
        'cadastro.Transportadora', related_name="venda_transportadora", on_delete=models.CASCADE, null=True, blank=True)
    veiculo = models.ForeignKey('cadastro.Veiculo', related_name="venda_veiculo",
                                on_delete=models.SET_NULL, null=True, blank=True)
    # Info
    data_emissao = models.DateField(null=True, blank=True)
    vendedor = models.CharField(max_length=255, null=True, blank=True)
    valor_total = models.DecimalField(max_digits=13, decimal_places=2, validators=[
                                      MinValueValidator(Decimal('0.00'))], default=Decimal('0.00'))

    @property
    def format_data_emissao(self):
        return '%s' % date(self.data_emissao, "d/m/Y")

    def get_valor_total_attr(self, nome_attr):
        valor_total = 0
        for item in self.itens_venda.all():
            v = getattr(item, nome_attr, 0)
            if v:
                valor_total += v

        return valor_total

    def __unicode__(self):
        s = u'Romaneio nº %s' % (self.id)

class VisualizarRomaneio(Carga):
    carga = models.ForeignKey(
        'vendas.OrcamentoVenda', related_name="orcamento_pedido",
        on_delete=models.SET_NULL, null=True, blank=True)
    data_entrega = models.DateField(null=True, blank=True)
    status = models.CharField(
        max_length=1, choices=STATUS_CARGA_ESCOLHAS, default='0')

    class Meta:
        verbose_name = "Romaneio"
        permissions = (
            ("ver_romaneio", "Pode visualizar romaneios"),
        )

    @property
    def format_data_entrega(self):
        return '%s' % date(self.data_entrega, "d/m/Y")

    @property
    def tipo_romaneio(self):
        return 'Romaneio'

    def edit_url(self):
        return reverse_lazy('vendas:editarpedidovendaview', kwargs={'pk': self.id})

    def __unicode__(self):
        s = u'Romaneio nº %s (%s)' % (
            self.id, self.get_status_display())
        return s

    def __str__(self):
        s = u'Romaneio nº %s (%s)' % (
            self.id, self.get_status_display())
        return s

Browser other questions tagged

You are not signed in. Login or sign up in order to post.