DCL_Dev
DCL_Dev

Reputation: 99

Django How do I show Author (user) name instea of ID?

I have a custom user model (CustomUsers) on one app called users, and then I have a model called Bookings from which I have created a ModelfForm called BookingsForm.

In the Bookings model (and ultimately the BookingsForm), I have a field called booking_author which has a ForeignKey inherited from the CustomUsers model.

Now I have been able to successfully call the booking_author field into my bookingscreate view and make it uneditable/read only as I had wanted. The issue now is that the field is displaying the id of the author instead of the name of the author. Is anyone able to help me resolve this?

views.py

@login_required
def bookingscreate(request):
    bookings_form = BookingsForm(initial={'booking_author': request.user })
    context = {
        'bookings_form': bookings_form
    }
    return render(request, 'bookings_create.html', context)

Upvotes: 2

Views: 732

Answers (1)

Rohan Thacker
Rohan Thacker

Reputation: 6337

There are multiple ways of getting the users name. To obtain the user name in this case you could use

request.user.get_full_name()
request.user.get_short_name()

Explanation:

request.user is the User object, and thus you have access to all user methods. If the above is not what you're looking for you could create a method in your user class and then call that method in this view.

Upvotes: 1

Related Questions