0

if I have a dictionary {{tokens}} being rendered in my Django template using that outputs:

{26: <Token: 0c90dd25e8bc07578725160349fd6fedac776afb>, 28: <Token: 6e3bb042eb4ea05918e26b80a2c6d7fada99cf86>, 29: <Token: ca4dc6ccc893605195002be986a07142d96f3371>, 42: <Token: 2f157eb152b818b5ca53d63344279626a73a4e38>}

I can access dictionary key 26 value in my template by using {{tokens.26}}

But how can I access these values if I am using a queryset to access it? For example, I want to do something like: {{tokens.machine.id}} where {{machine.id}} is equal to 26. This doesn't work. I have also tried:

{% with machine.id as machineID %}
{{ tokens.machineID }}
{% endwith %}

This outputs nothing.

EDIT Added Models, view, and template

models:

class CustomUser(AbstractUser):
    PSScustomer= models.ForeignKey(Customer, on_delete=models.CASCADE)
    customerTag = models.CharField(max_length=50,) 
    isAdmin = models.BooleanField(default = False,)
    isSuperUser = models.BooleanField(default = False,)
    activated = models.BooleanField(default = False,) 

view

class equipmentdashboardView(LoginRequiredMixin,ListView):
    context_object_name = 'equipmentdashboard'
    template_name = 'equipmentdashboard.html' 
    login_url = 'login'

    def get_queryset(self):
        if self.request.user.is_superuser: #so that admin can see all machines
            machineQuery = CustomUser.objects.filter(isDevice=True).order_by('order')
            groups = machineQuery.values_list('group', flat=True)
            uniqueList = []
            machineDict = {}
            for group in groups:
                if group not in uniqueList:
                    uniqueList.append(group)
            for group in uniqueList:
                machineDict[group] = CustomUser.objects.filter(group = group, isDevice=True).order_by('order')
            return machineDict
        else:
            machineQuery = CustomUser.objects.filter(PSScustomer=self.request.user.PSScustomer).filter(isDevice=True).order_by('order')
            groups = machineQuery.values_list('group', flat=True)
            uniqueList = []
            machineDict = {}
            for group in groups:
                if group not in uniqueList:
                    uniqueList.append(group)
            for group in uniqueList:
                machineDict[group] = CustomUser.objects.filter(PSScustomer=self.request.user.PSScustomer, group = group, isDevice=True).order_by('order')
            return machineDict


    def get_context_data(self, **kwargs):
        context = super(equipmentdashboardView, self).get_context_data(**kwargs)
        assets = CustomUser.objects.filter(PSScustomer=self.request.user.PSScustomer, isDevice = True)
        tokens = {}
        for asset in assets:
            tokens[asset.id] = Token.objects.get(user=asset)
        context['tokens'] = tokens
        return context

template

{% for group, machines in equipmentdashboard.items %}
    {% for machine in machines %}
        <p id="serial">{{ machine.deviceSerial }}</p>
        <p id="token">{{ tokens.machine.id}}</p>
    {% endfor %}
{% endfor %}
MattG
  • 1,682
  • 5
  • 25
  • 45

1 Answers1

1

The best option I found that worked was using a property, so that I can just access the token just like any other model field. So:

class CustomUser(AbstractUser):
    PSScustomer= models.ForeignKey(Customer, on_delete=models.CASCADE)
    customerTag = models.CharField(max_length=50,) 
    isAdmin = models.BooleanField(default = False,)
    isSuperUser = models.BooleanField(default = False,)
    activated = models.BooleanField(default = False,) 

    @property
    def token(self):
       return Token.objects.get(user=self)
MattG
  • 1,682
  • 5
  • 25
  • 45