getattr(): attribute name must be string

163 views
Skip to first unread message

David Crandell

unread,
Jun 12, 2021, 1:08:56 AM6/12/21
to Django users
Hello,

I am trying to migrate changes to my models. When I do, I get a traceback error to my Employee model which says 

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\guita\Documents\oh_new\venv\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:\Users\guita\Documents\oh_new\venv\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    django.setup()
  File "C:\Users\guita\Documents\oh_new\venv\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\guita\Documents\oh_new\venv\lib\site-packages\django\apps\registry.py", line 114, in populate
    app_config.import_models()
  File "C:\Users\guita\Documents\oh_new\venv\lib\site-packages\django\apps\config.py", line 301, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\guita\Documents\ohmg_new\ohmg\models.py", line 5, in <module>
    from customers.models import CustomerLogos, Customers
  File "C:\Users\guita\Documents\ohmg_new\customers\models.py", line 5, in <module>
    from employees.models import Employee
  File "C:\Users\guita\Documents\ohmg_new\employees\models.py", line 6, in <module>
    class Employee(models.Model):
  File "C:\Users\guita\Documents\oh_new\venv\lib\site-packages\django\db\models\base.py", line 161, in __new__
    new_class.add_to_class(obj_name, obj)
  File "C:\Users\guita\Documents\oh_new\venv\lib\site-packages\django\db\models\base.py", line 326, in add_to_class
    value.contribute_to_class(cls, name)
  File "C:\Users\guita\Documents\oh_new\venv\lib\site-packages\django\db\models\fields\__init__.py", line 788, in contribute_to_class
    if not getattr(cls, self.attname, None):
TypeError: getattr(): attribute name must be string

________________________________________________________

This is the model it's fussing about.

class Employee(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.EmailField()
password = models.CharField(max_length=50)
addr1 = models.CharField(max_length=50)
addr2 = models.CharField(max_length=50)
city = models.CharField(max_length=75)
state = models.CharField(max_length=75)
zip = models.CharField(max_length=10)
position = models.CharField(max_length=50)
date_hired = models.DateTimeField(default=timezone.now)
date_updated = models.DateTimeField(auto_now=True)
date_terminated = models.DateTimeField(default=None)
is_active = models.BooleanField(default='1')
status = models.SmallIntegerField(default=0)
emp_is_salary = models.BooleanField(default='0')
emp_pto_rate = models.FloatField(2, 6)
emp_user_level = models.SmallIntegerField(default=1)
emerg_contact1 = models.CharField(max_length=100)
emerg_contact1_phone = models.CharField(max_length=20)
emerg_contact1_address = models.CharField(max_length=200)
emerg_contact1_city_st = models.CharField(max_length=200)
emerg_contact2 = models.CharField(max_length=100)
emerg_contact2_phone = models.CharField(max_length=20)
emerg_contact2_address = models.CharField(max_length=200)
emerg_contact2_city_st = models.CharField(max_length=200)
emp_note = models.TextField()
emp_hourly_rate = models.FloatField(2, 2)
emp_net_pto = models.FloatField(2, 8)
emp_pto_prev = models.FloatField(2, 8)

def __str__(self):
return self.first_name

David Crandell

unread,
Jun 12, 2021, 8:59:58 AM6/12/21
to Django users
It was a field type mismatch. I should have used DecimalField() instead of FloatField()
Reply all
Reply to author
Forward
0 new messages