Jives Jives - 1 year ago 57
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?


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
AttributeError: 'User' object has no attribute 'update'


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


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:
return (True, new_user.id)

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

Answer Source

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'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download