-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added ability for username
to act as identifier for API user detail endpoint in addition to primary key (/api/users/<userID>
OR /api/users/<username>
)
#321
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see me comment on conflicting query options
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if a user goes by the name "1234"? That could cause an issue with searching for the correct user. A better implementation could have you search through user name first and store usernames as "User#user_id" That way you can query id numbers by using the #, and we can exclude # from potential username characters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just made a commit that validates the User's username
, and ensures that it cannot be fully numeric. This way, we will not have this problem, as a given URL parameter can only be interpreted as either a username
OR an id
but not BOTH.
NOTE: This commit is in PR #291, because in that PR, I created the username field for the User
model, so it is only consistent that I put restrictions on the username
field in that PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You must reach out to the users team before I will approve any changes to the User
model.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the changes!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def get_object(self): | ||
lookup_value = self.kwargs.get(self.lookup_field) | ||
|
||
# If the lookup_value is an integer, use the id field |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good code comment!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works as expected!
I can POST users with unique usernames/email while having overlapping names, then go to api/users/<username>
and get their detail view.
While still being able to access api/users/<pk>
Great work and keep moving forward! We're almost done!
Issue Score: Excellent Comments: |
Closing Comment: Successfully, completed the |
Goal: In this PR, we allow users of the API to access the Detail view of a given
User
object using the user'susername
as a slug instead of always having to use the user'sid
. For a user of the API to get a givenUser's ID
, they need to go through the entire List view of user's and find the specificUser
they are looking for and then look at theid
of thatUser
.The
username
on the other hand, is a unique identifier for aUser
object, that is human readable and can be shared/accessed easily. This way, it is easier for someone that uses the API to get details of a specific user, as one does not need to know it's primary key in the model.We will support:
/api/users/<user_id>
AND/api/users/<username>
to GET/PATCH/DELETE the Detail view of theUser
with the givenid
orusername