Jives Jives - 21 days ago 6
Python Question

Getting object has no attribute error when creating admin reg in django

I have been getting this error only when creating my first user, which is suppose to become the admin user. How can i fix this?

Error:

response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/Johnny/Desktop/pythondjango/django/user_dashboard/apps/login/views.py", line 18, in register
regstatus = User.userManager.register(**request.POST)
File "/Users/Johnny/Desktop/pythondjango/django/user_dashboard/apps/login/models.py", line 43, in register
new_user.update(user_level='admin')
AttributeError: 'User' object has no attribute 'update'


code:
views.py

def register(request):
regstatus = User.userManager.register(**request.POST)
if regstatus[0]:
request.session['user_id'] = regstatus[1]
return redirect(reverse('dashboard:index'))
else:
for message in regstatus[1]:
messages.warning(request, message)
return redirect(reverse('login:index'))


models.py:

new_user = self.create(email=email, first_name=first_name, last_name=last_name, password=pw_hash)
# Make first registered user an admin
if new_user.id == 1:
new_user.update(user_level='admin')
return (True, new_user.id)


These two snippets of code is where my errors are currently happening. Any help is greatly appreciated

Answer

If you just want to change a obj's field value, you should just assign it as attribute and then call .save() on it.

new_user.user_level = 'admin'
new_user.save()