Home > Net >  In Django, if my request returns a username, can I also get the user's first and last name?
In Django, if my request returns a username, can I also get the user's first and last name?

Time:12-23

I'm building a Django app for a law firm to let them assign incoming cases to the firm's lawyers. The lawyers are the users, so I used a list of the lawyers to make a dropdown of their usernames, so it's easy to select an active lawyer for the assignment.

In some of the templates I create, it's not hard to show the full name of the assigned lawyer. I use "client.assignee.get_full_name", and it shows "John Attorney".

 I use "client.assignee.get_full_name", and it shows "John Attorney".

But that's a piece of data that seems to ride along with the Client model.

I can also get first and last names in my menu dropdowns by querying the list of attorneys through a context processor:

def attorney_menu(request):
    return {
    'attorneys': User.objects.filter(groups__name='attorney-staff')
    }

enter image description here

The code to produce that is:

 <ul >
      {% if attorneys %}
          {% for attorney in attorneys %}
              <li><a href="/by_attorney?username={{ attorney.username }}">{{ attorney.first_name }} {{ attorney.last_name }}</a></li>
          {% endfor %}
       {% endif %}
 </ul>

All the above is working fine.

However, in the admin, in the templates created by the default Django admin, my dropdowns can only show the username ("johnattorney" instead of "John Attorney"). Also in the attorney pages (the pages that show the attorney's individual assigned clients), I can only show the username.

in the attorney pages, I can only show the username

The code to produce that is

    <h1>Clients assigned to {% if request.GET.username %}{{ request.GET.username }}{% endif %}</h1>

Here, I'd like to access that "attorneys" query, but I only seem to have the request, which just returns a username.

My question, then, is this: In the Admin context, or in the context of a page based on the Client model that only knows about the user name as a value of "assignee" as part of the model, can I get the first and last name to display instead of the username?

CodePudding user response:

I was able to reuse the context processor to get the full name into the By Attorney page heading, like this:

<h1>Clients assigned to 
    {% if attorneys %}
    {% for attorney in attorneys %}
        {% if attorney.username == request.GET.username %}
      {{ attorney.first_name }} {{ attorney.last_name }}
        {% endif %}
    {% endfor %}
  {% endif %}
  </h1>

The context processor, by the way, is just:

def attorney_menu(request):
    return {
    'attorneys': User.objects.filter(groups__name='attorney-staff')
    }

As for changing the dropdowns in the admin, I guess that's an entirely separate question. I'm using the stock Django admin, no custom views, and I think that will be a harder change to make.

CodePudding user response:

Yes first you would have to define a method under the model that you what to get the full name.

class somemodel(models.Model):
     username = models.CharField(max_length=100)
     first_name = models.CharField(max_length=100)
     last_name = models.CharField(max_length=100)
     

     # then you get the names through the method 
     def get_full_name(self):
        full_name = "%s %s" % (self.first_name, self.last_name)
        return full_name

You can just simply call both names with the method attribute in your Hmtl like this

 <ul >
      {% if attorneys %}
          {% for attorney in attorneys %}
              <li><a href="/by_attorney?username={{ attorney.username.get_full_name }}"></a></li>
          {% endfor %}
       {% endif %}
 </ul>
  • Related