Having TypeError: 'CharField' object is not callable after migrating with django

432 views Asked by At

This is my models.py from my app, the django version is 4.0.4, my python version is 3.10.4. I made all of my models and after making makemigrations in order to generate the database it gives an error code.

from enum import unique
from django.db import models
from django.utils import timezone
from django.core.validators import MinValueValidator, MaxValueValidator

# Create your models here.
class I_Articulos(models.Model):
    pais = models.CharField('Pais', max_length=30, null=False, blank=False)
    grupo_economico = models.CharField('Grupo_Economico', max_length=30, null=False, blank=False)
    empresa = models.CharField('Empresa', max_length=30, null=False, blank=False)
    sucursal = models.CharField('Sucursal', max_length=30, null=False, blank=False)
    almacenado = models.CharField('Almacenado', max_length=30, null=False, blank=False)
    id_rubro = models.FloatField('id_rubro', null=False, blank=False, default=0, validators=[MinValueValidator(0), MaxValueValidator(99999)], unique=True)
    descuento_maximo = models.CharField = models.CharField('Descuento_Maximo', max_length=20, null=False, blank=False)
    descuento_directo = models.CharField('Descuento_Directo', max_length=20, null=False, blank=False)
    unidad_minima_venta = models.DecimalField('Unidad_Minima_Venta', max_digits=15, decimal_places=4, null=False, blank=False)
    cantidad_unidades_unidad_venta = models.DecimalField('Cant_U_UV', max_length=30, null=False, blank=False)
    unidad_venta = models.CharField('Unidad_Venta', max_length=30, null=False, blank=False)
    descripcion_articulo = models.CharField('Descripcion_Articulo', max_length=100, null=False, blank=False)
    codigo_articulo = models.CharField('Codigo_articulo', max_length=30, null=False, blank=False)
    codigo_busqueda = models.CharField('Codigo_busqueda', max_length=30, null=False, blank=False)
    id_divisiona = models.CharField('id_divisiona', max_length=20, null=False, blank=False)
    id_segmento = models.CharField('id_segmento', max_length=20, null=False, blank=False)
    id_subrublo = models.CharField('id_subrublo', max_length=20, null=False, blank=False)
    id_linea = models.CharField('Pais', max_length=20, null=False, blank=False)
    tipo_item = models.PositiveIntegerField('tipo_item', null=False, blank=False, default=0, validators=[MinValueValidator(0), MaxValueValidator(99999)])
    combo_calcula_stock = models.CharField('combo_calcula_stock', max_length=30, null=False, blank=False)
    vc_fechahora_ultima_modificacion = models.DateTimeField('vc_fechahora_ultima_modificacion', null=False, blank=False)
    status = models.PositiveIntegerField('Status', null=False, blank=False, default=0, validators=[MinValueValidator(0), MaxValueValidator(99999)])
    activo = models.CharField('Activo', max_length=1, null=False, blank=False)
    fechahora_ultima_modificacion = models.DateTimeField('Pais', null=False, blank=False)

Then I run python3 manage.py makemigrations to generate my database, but gives me this error:

(VENV) byakenda@byakenda-G5-5587:~/Documents/somewhere/trucksys$ python3 manage.py makemigrations
Traceback (most recent call last):
  File "/home/somewhere/trucksys/manage.py", line 22, in <module>
    main()
  File "/home/somewhere/trucksys/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/somewhere/VENV/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/home/somewhere/VENV/lib/python3.10/site-packages/django/core/management/__init__.py", line 420, in execute
    django.setup()
  File "/home/somewhere/VENV/lib/python3.10/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/somewhere/VENV/lib/python3.10/site-packages/django/apps/registry.py", line 116, in populate
    app_config.import_models()
  File "/home/somewhere/VENV/lib/python3.10/site-packages/django/apps/config.py", line 304, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/somewhere/trucksys/truck/models.py", line 7, in <module>
    class I_Articulos(models.Model):
  File "/home/somewhere/trucksys/truck/models.py", line 15, in I_Articulos
    descuento_directo = models.CharField('Descuento_Directo', max_length=20, null=False, blank=False)
TypeError: 'CharField' object is not callable

What should I do to fix this issue? What is related to? I'm a Jr. Python Developer so it would be helpful if some of you with some experience or even experienced could help me get this through.

2

There are 2 answers

0
Sherif Hassan On BEST ANSWER

check the 7th model field , 'models.CharField =' i assume is a typo?

descuento_maximo = models.CharField = models.CharField('Descuento_Maximo', max_length=20, null=False, blank=False)
0
willeM_ Van Onsem On

The following line is the culprit:

descuento_maximo = models.CharField = models.CharField('Descuento_Maximo', max_length=20, null=False, blank=False)

you here assign a new value to models.CharField, such that it is no longer a reference to the CharField class, but a reference to a CharField object. You should remove the = models.CharField part, so:

descuento_maximo = models.CharField('Descuento_Maximo', max_length=20, null=False, blank=False)

It is not necessary however to specify null=False and blank=False, this is by default the case.

class IArticulos(models.Model):
    pais = models.CharField('Pais', max_length=30)
    grupo_economico = models.CharField('Grupo_Economico', max_length=30)
    empresa = models.CharField('Empresa', max_length=30)
    sucursal = models.CharField('Sucursal', max_length=30)
    almacenado = models.CharField('Almacenado', max_length=30)
    id_rubro = models.FloatField('id_rubro', default=0, validators=[MinValueValidator(0), MaxValueValidator(99999)], unique=True)
    descuento_maximo = models.CharField('Descuento_Maximo', max_length=20)
    descuento_directo = models.CharField('Descuento_Directo', max_length=20)
    unidad_minima_venta = models.DecimalField('Unidad_Minima_Venta', max_digits=15, decimal_places=4)
    cantidad_unidades_unidad_venta = models.DecimalField('Cant_U_UV', max_length=30)
    unidad_venta = models.CharField('Unidad_Venta', max_length=30)
    descripcion_articulo = models.CharField('Descripcion_Articulo', max_length=100)
    codigo_articulo = models.CharField('Codigo_articulo', max_length=30)
    codigo_busqueda = models.CharField('Codigo_busqueda', max_length=30)
    id_divisiona = models.CharField('id_divisiona', max_length=20)
    id_segmento = models.CharField('id_segmento', max_length=20)
    id_subrublo = models.CharField('id_subrublo', max_length=20)
    id_linea = models.CharField('Pais', max_length=20)
    tipo_item = models.PositiveIntegerField('tipo_item', default=0, validators=[MinValueValidator(0), MaxValueValidator(99999)])
    combo_calcula_stock = models.CharField('combo_calcula_stock', max_length=30)
    vc_fechahora_ultima_modificacion = models.DateTimeField('vc_fechahora_ultima_modificacion')
    status = models.PositiveIntegerField('Status', default=0, validators=[MinValueValidator(0), MaxValueValidator(99999)])
    activo = models.CharField('Activo', max_length=1)
    fechahora_ultima_modificacion = models.DateTimeField('Pais')

Note: Models in Django are written in PascalCase, not snake_case, so you might want to rename the model from I_Articulos to IArticulos.