Skip to main content

Update User

Update an existing user.

HTTP Request

PATCH/api/users/:id

Authorization

Authorization

  • Required: Yes
  • Permission: Staff or Admin
  • Authentication: Token-based (Authorization: Token <your_api_key>)

Path Parameters

ParameterTypeRequiredDescription
idIntegerYesUnique ID of the user to update

Request Body

FieldTypeRequiredDescription
usernameStringNoUnique username for the user.
mobile_numberStringNoMobile phone number of the user.
emailStringNoEmail address of the user (must be unique).
first_nameStringNoFirst name of the user.
last_nameStringNoLast name of the user.
genderStringNoGender of the user (male, female, non-binary).
is_staffBooleanNoWhether user has staff privileges.
is_adminBooleanNoWhether user has admin privileges.
is_activeBooleanNoWhether user account is active.
groupsArrayNoList of group IDs the user belongs to.
avatarFileNoAvatar image file for the user.

Example Requests

1import requests
2
3api = requests.Session()
4api.headers.update({'Authorization': 'Token f4e75eab6e0f663a972d145478d6fc4b81762070'})
5response = api.patch(
6  'http://www.example.com/api/users/78/',
7  json={
8      'first_name': 'Michael',
9      'last_name': 'Chen-Rodriguez',
10      'mobile_number': '+1-555-0157',
11      'is_staff': True,
12      'groups': [1, 3, 5],
13      'avatar': open('path/to/avatar.jpg', 'rb')
14  }
15)

Status Codes

CodeDescription
200User updated successfully
400Bad request — invalid input
401Unauthorized — authentication required
403Forbidden — insufficient permissions
404Not found — user does not exist
500Internal server error

Response Fields

FieldTypeDescription
idIntegerUnique ID of the user.
usernameStringUsername of the user.
mobile_numberStringMobile phone number of the user.
emailStringEmail address of the user.
avatarStringURL to the user's avatar image (null if none).
first_nameStringFirst name of the user.
last_nameStringLast name of the user.
full_nameStringFull name (first + last name).
genderStringGender of the user.(male/female)
is_staffBooleanWhether user has staff privileges.
is_adminBooleanWhether user has admin privileges.
is_activeBooleanWhether user account is active.
last_loginString (ISO 8601)Timestamp of last login (null for new users).
created_atString (ISO 8601)Timestamp when user was created.
updated_atString (ISO 8601)Timestamp when user was last updated.
is_onlineBooleanWhether user is currently online.
groups_dataArray[Object]Array of group objects with detailed info.

Group Data Structure

FieldTypeDescription
idIntegerUnique ID of the group
nameStringName of the group

Example Response

{
"id": 42,
"username": "michael_chen",
"mobile_number": "+1-555-0156",
"email": "michael.chen@techcorp.com",
"first_name": "Michael",
"last_name": "Chen",
"full_name": "Michael Chen",
"gender": "male",
"avatar": "http://www.example.com/media/avatars/michael.jpg",
"is_staff": true,
"is_admin": false,
"is_active": true,
"last_login": null,
"created_at": "2023-01-01T12:00:00Z",
"updated_at": "2023-01-01T12:00:00Z",
"is_online": false,
"groups_data": [
{
"id": 1,
"name": "Admins"
},
{
"id": 3,
"name": "Editors"
}
]
}

Notes

    • Set null value to avatar field to remove existing avatar.