Need to display status text not value

In my model I have status

class CustomUser(AbstractUser):
    alias_email = models.EmailField(null=True, blank=True)
    alias_name = models.CharField(max_length=20, unique=True, null=True, blank=True)
    cell = models.CharField(blank=True, max_length=10, help_text='Cell phone number (numbers only)')
    downline = models.CharField(tuple, max_length=1000000, default=1)
    email_confirmed = models.BooleanField(default=False)
    org_agreement_accept = models.BooleanField(default=False)
    sponsor = models.BigIntegerField(default=1)
    sponsor_accept = models.BooleanField(default=False, null=True, blank=True)
    upline = models.CharField(tuple, max_length=1000000, default=1)
    org = models.ForeignKey(Organization, on_delete=models.SET_NULL, null=True)

    Status = (
        (1, 'Fish'),
        (2, 'FAS'),
        (3, 'Fisher'),
        (4, 'OSA'),
        (5, 'Org Crew'),
        (6, 'Org Admin'),
        (7, 'Org Owner'),
    )

    status = models.SmallIntegerField(choices=Status, default=1)

    class Meta:
        ordering = ['alias_name', 'org', 'username', 'email', 'cell', 'sponsor', 'org_id', 'status'] 


    def __str__(self):
        return f'{self.alias_name}, {self.org}, {self.username}, {self.email}, {self.cell}, {self.sponsor}, {self.org}, {self.status}' 

I use the numbers for queries but want to display the name.

Since you are using a tuple of tuples for this information, you can do this one of two ways.

You can either search through the list of 2-tuples to find the first entry containing the desired value. Or you can convert the tuples to a dict and directly reference the key in the dict.

Sample implementation of the first case:

label = [
    entry[1] for entry in Stat
    if entry[0] == status
][0]

Sample implementation of the second:

label = dict(Status)[status]

(Obviously, the precise implementation you need will depend upon the specific context in which you’re going to use it.)

ok this gets me close

    leadership = CustomUser.objects.filter(sponsor=user.id, status__range=(5, 6))
    for status in leadership:
        status_id = leadership.values('status')
        status = dict(CustomUser.Status).pop(status_id)
        print(status)

gives me this error

KeyError at /accounts/org_leadership/

<QuerySet [{'status': 5}, {'status': 6}]>

So now I just need to get the numbers only

query

    leadership = CustomUser.objects.filter(sponsor=user.id, status__range=(5, 6))
    for status in leadership:
        status = dict(CustomUser.Status).pop(status.status)
        print(status)

print - which is correct for template
Org Admin
Org Crew

template

<td> {{ status }} </td>

result
image

output

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
</head>
<body>

    <nav class="navbar navbar-primary bg-primary flex-row">

          <a class="navbar-brand" href="#">
            <img src="/media/" alt="" width="60" height="30">
          </a>


    <ul class="nav navbar-dark bg-primary flex-row-reverse">
      <li class="nav-item">
           <a class='nav-link text-white' href="/accounts/qrcode_button/">Fish QRCode</a>
      </li>
      <li class="nav-item">
          <a class='nav-link text-white' href="/logout/">Logout</a>
      </li>
      <li class="nav-item">
        <a class='nav-link text-white' href="/login/">Login</a>
      </li>
      <li class="nav-item ">
        <a class='nav-link text-white' href="#">Donate</a>
      </li>
      <li class="nav-item ">
        <a class='nav-link text-white' href="/accounts/index/">Home</a>
      </li>
    </ul>
  </div>
  </nav>

  
    <div class="container mt-2">
    <h2>Jeff Group</h2>
      <table class="table table-striped table-bordered">
        <thead class="thread-dark thread-center" >
            <tr>
                <th scope="col">Crew</th>
                <th scope="col">Status</th>
                <th scope="col">Email</th>
                <th scope="col">Text(coming soon)</th>
                <th scope="col">Other Actions</th>
            </tr>
        </thead>
        <tbody>
          
            <tr>
                <td>cara_l1234</td>
                <td hidden>35</td>
                <td> Org Crew </td>
                <td><a href="/accounts/email/35/2/" class="btn btn-primary">Email</button></a></td>
                <td><a href="#" class="btn btn-success disabled">Send Text</button></td>
                <td>
                <div class="dropdown">
                  <button class="btn btn-dark dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
                    Dropdown button
                  </button>
                  <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
                    <li><a class="dropdown-item" href="#">No Actions</a></li>
                  </ul>
                </div>
                </td>
            </tr>
            
            <tr>
                <td>jeff_u2442</td>
                <td hidden>32</td>
                <td> Org Crew </td>
                <td><a href="/accounts/email/32/2/" class="btn btn-primary">Email</button></a></td>
                <td><a href="#" class="btn btn-success disabled">Send Text</button></td>
                <td>
                <div class="dropdown">
                  <button class="btn btn-dark dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
                    Dropdown button
                  </button>
                  <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
                    <li><a class="dropdown-item" href="#">No Actions</a></li>
                  </ul>
                </div>
                </td>
            </tr>
            
        </tbody>
        </table> 
      </div>
    
</div>
<!-- Footer -->
<footer class="text-center text-lg-start bg-white text-muted">
  <!-- Section: Social media -->
  <section class="d-flex justify-content-center justify-content-lg-between p-4 border-bottom">
    <!-- Left -->
    <div class="me-5 d-none d-lg-block">

    </div>
    <!-- Left -->

    <!-- Right -->

  
    <!-- Right -->
  </section>
  <!-- Section: Social media -->

  <!-- Section: Links  -->
  <section class="">
    <div class="container text-center text-md-start mt-5">
      <!-- Grid row -->
      <div class="row mt-3">
        <!-- Grid column -->
        <div class="col-md-3 col-lg-4 col-xl-3 mx-auto mb-4">
          <!-- Content -->
          <h6 class="text-uppercase fw-bold mb-4">
            <i class="fas fa-gem me-3 text-secondary"></i>Company name
          </h6>
          <p>
            Here you can use rows and columns to organize your footer content. Lorem ipsum
            dolor sit amet, consectetur adipisicing elit.
          </p>
        </div>
        <!-- Grid column -->

        <!-- Grid column -->
        <div class="col-md-2 col-lg-2 col-xl-2 mx-auto mb-4">
          <!-- Links -->
          <h6 class="text-uppercase fw-bold mb-4">
            Products
          </h6>
          <p>
            <a href="#!" class="text-reset">Angular</a>
          </p>
          <p>
            <a href="#!" class="text-reset">React</a>
          </p>
          <p>
            <a href="#!" class="text-reset">Vue</a>
          </p>
          <p>
            <a href="#!" class="text-reset">Laravel</a>
          </p>
        </div>
        <!-- Grid column -->

        <!-- Grid column -->
        <div class="col-md-3 col-lg-2 col-xl-2 mx-auto mb-4">
          <!-- Links -->
          <h6 class="text-uppercase fw-bold mb-4">
            Useful links
          </h6>
          <p>
            <a href="#!" class="text-reset">Pricing</a>
          </p>
          <p>
            <a href="#!" class="text-reset">Settings</a>
          </p>
          <p>
            <a href="#!" class="text-reset">Orders</a>
          </p>
          <p>
            <a href="#!" class="text-reset">Help</a>
          </p>
        </div>
        <!-- Grid column -->

        <!-- Grid column -->
        <div class="col-md-4 col-lg-3 col-xl-3 mx-auto mb-md-0 mb-4">
          <!-- Links -->
          <h6 class="text-uppercase fw-bold mb-4">Contact</h6>
          <p><i class="fas fa-home me-3 text-secondary"></i> New York, NY 10012, US</p>
          <p>
            <i class="fas fa-envelope me-3 text-secondary"></i>
            info@example.com
          </p>
          <p><i class="fas fa-phone me-3 text-secondary"></i> + 01 234 567 88</p>
          <p><i class="fas fa-print me-3 text-secondary"></i> + 01 234 567 89</p>
        </div>
        <!-- Grid column -->
      </div>
      <!-- Grid row -->
    </div>
  </section>
  <!-- Section: Links  -->

  <!-- Copyright -->
  <div class="text-center p-4" style="background-color: rgba(0, 0, 0, 0.025);">
    © 2021 Copyright:
    <a class="text-reset fw-bold" href="https://mdbootstrap.com/">MDBootstrap.com</a>
  </div>
  <!-- Copyright -->
</footer>
<!-- Footer -->
</body>
</html>

I think the issue is that I am not putting the status with leadership when it is sent over so it is just picking one in the template.

This

def org_leadership(request):
    user = CustomUser.objects.filter(id=request.user.id).get()
    org = user.org
    leadership = CustomUser.objects.filter(sponsor=user.id, status__range=(5, 6))
    for status in leadership:
        status_name = dict(CustomUser.Status).pop(status.status)
        status = str(status) + ', ' + status_name
        print(status)

Gets this

cara_l1234, Jeff Group, clovern01, x, 5, 2, Jeff Group, 6, Org Admin
jeff_u2442, Test Group, jeffy, x, 2, 2, Test Group, 5, Org Crew

Now to just pass that to template in a way I can reference

Got this to the template

{'leader': [{'alias_name': 'cara_l1234', 'leader_id': 35, 'status_name': 'Org Admin'}, {'alias_name': 'jeff_u2442', 'leader_id': 32, 'status_name': 'Org Crew'}]}

Trying to find out what the for loop needs in html

Got it to work.

template

{% for leader in leadership %}
            <tr>
                <td>{{ leader.0 }}</td>
                <td hidden>{{ leader.1 }}</td>
                <td> {{ leader.2 }} </td>

view

leadership_query = CustomUser.objects.filter(sponsor=user.id, status__range=(5, 6))

    leadership = []
    for status in leadership_query:
        leader = []
        alias_name = status.alias_name
        leader_id = status.id
        status_name = str(status) and dict(CustomUser.Status).pop(status.status)

        leader.append(alias_name)
        leader.append(leader_id)
        leader.append(status_name)
        leadership.append(leader)

You’re still doing a lot more work here than you need to.

You don’t need to create the dict in every iteration of the loop and you don’t want to pop the status from the dict.

You also don’t need to create a set of nested lists for your data.

This code can be simplified to:

    leadership_query = list(CustomUser.objects.filter(sponsor=user.id, status__range=(5, 6)))
    status_dict = dict(CustomUser.Status)
    for status in leadership_query:
        status.status_name = status_dict[status.status]

You then reference the individual fields by name within the template.

Welp that is a lot easier lol