Repository: netbox-community/netbox
Pull request: Closes #8894: Add first and last name username user api select
<!--
    Thank you for your interest in contributing to NetBox! Please note
    that our contribution policy requires that a feature request or bug
    report be opened for approval prior to filing a pull request. This
    helps avoid wasting time and effort on something that we might not
    be able to accept.

    Please indicate the relevant feature request or bug report below.
    IF YOUR PULL REQUEST DOES NOT REFERENCE AN ACCEPTED BUG REPORT OR
    FEATURE REQUEST, IT WILL BE MARKED AS INVALID AND CLOSED.
-->
### Fixes: #8894
<!--
    Please include a summary of the proposed changes below.
-->


Problem statement:
Show full usernames when filtering
### NetBox version

v3.1.9

### Feature type

Change to existing functionality

### Proposed functionality

When using the filters to filter user-based (e.g. Journal and Changelog), the full user name should be displayed in brackets, instead of just the login name.

### Use case

Makes filtering in big installations easier

### Database changes

None, all data is already in netbox

### External dependencies

None

Hints from issue discussion:
Currently:
![grafik](https://user-images.githubusercontent.com/18295403/158780465-0b1fbbdb-79f4-46cd-b349-4208b2cf7037.png)
Proposed:
![Unbenannt](https://user-images.githubusercontent.com/18295403/158782389-90a151f6-3caf-4f39-bdec-c3b6ca48506b.png)

Commit message: Closes #8894: Add first and last name to APISelect widget if set

Patch to review:
diff --git a/netbox/users/api/nested_serializers.py b/netbox/users/api/nested_serializers.py
index df9af0f1911..3b4959a1e01 100644
--- a/netbox/users/api/nested_serializers.py
+++ b/netbox/users/api/nested_serializers.py
@@ -23,11 +23,17 @@ class Meta:
 
 class NestedUserSerializer(WritableNestedSerializer):
     url = serializers.HyperlinkedIdentityField(view_name='users-api:user-detail')
+    display = serializers.SerializerMethodField(read_only=True)
 
     class Meta:
         model = User
         fields = ['id', 'url', 'display', 'username']
 
+    def get_display(self, obj):
+        if obj.first_name and obj.last_name:
+            return f"{obj.username} ({obj.first_name} {obj.last_name})"
+        return obj.username
+
 
 class NestedTokenSerializer(WritableNestedSerializer):
     url = serializers.HyperlinkedIdentityField(view_name='users-api:token-detail')


Code review comments:
diff_hunk: @@ -23,11 +23,17 @@ class Meta:
 
 class NestedUserSerializer(WritableNestedSerializer):
     url = serializers.HyperlinkedIdentityField(view_name='users-api:user-detail')
+    display = serializers.SerializerMethodField(read_only=True)
path: netbox/users/api/nested_serializers.py
text: @user1:
This is defined on the `BaseModelSerializer` class.

```suggestion
```

diff_hunk: @@ -23,11 +23,17 @@ class Meta:
 
 class NestedUserSerializer(WritableNestedSerializer):
     url = serializers.HyperlinkedIdentityField(view_name='users-api:user-detail')
+    display = serializers.SerializerMethodField(read_only=True)
 
     class Meta:
         model = User
         fields = ['id', 'url', 'display', 'username']
 
+    def get_display(self, obj):
+        if obj.first_name and obj.last_name:
+            return f"{obj.username} ({obj.first_name} {obj.last_name})"
path: netbox/users/api/nested_serializers.py
text: @user1:
We can use `get_full_name()` on the User instance to return the first and/or last name if either is defined:

```suggestion
        if full_name := obj.get_full_name():
            return f"{obj.username} ({full_name})"
```

@user1:
`get_display()` should be defined on `UserSerializer` as well.