local variable 'type' referenced before assignment, when using custom filter

Hello,
i have a created a custom filter to change the output of field shown in a template:

@register.filter(is_safe=True)
@stringfilter
def convert_type(value):
    """Convert name in database to template format"""
    if value == CongePaye.choix[0][0]:
        type = CongePaye.nom_element
    elif value == CongeNonPaye.choix[0][0]:
        type = CongeNonPaye.nom_element
    elif value == CongeRecuperation.choix[0][0]:
        type = CongeRecuperation.nom_element
    elif type in TypeConge.objects.all().name:
         type = value.capitalize()
    else:
        type = value.capitalize()
    return type

when i apply the filter it gives me the error:

local variable 'type' referenced before assignment, in line 106

104 	                    </td>
105 	                    <td>{{ c.demandeur }}</td>
106 	                    <td>{{ c.type | convert_type}}</td>
107 	                    <td>{{ c.date_creation|date:"d/m/Y" }}</td>
108 	                    <td>{{ c.status | convert_status }}</td>
109 	                    <td>{{ c.start_date }}</td>
110 	                    <td>{{c.demiJ_debut | convert_debut_fin}}</td>
111 	
112 	                    <td>{{ c.end_date }}</td>
113 	                    <td>{{c.demiJ_fin | convert_debut_fin}}</td>
114 	                    <td>pp</td>

when i remove the “convert_type” filter the template works fine. and the weird is that when i apply it works for the other models where the field type is not forgeinkey.

this is the models for the classes:

class CongePaye(Conge):
    choix = [("paye","Congé Payé")]
    type = models.CharField(max_length=25, choices=choix, default=choix[0], verbose_name="Type")

class CongeNonPaye(Conge):
    choix = [("nonpaye","Non Payé")]
    type = models.CharField(max_length=25, choices=choix, default=choix[0], verbose_name="Type")

class CongeExceptionnel(Demande):
    type = models.ForeignKey(TypeConge, on_delete=models.SET_NULL, null=True)

i apologize if i make a mistake in the post this is my first Topic in this forum.

You want to put your code inside ```: Markup - The Unofficial Django Discord

I highly recommend not using type as a variable name as that’s a builtin.

elif type in TypeConge.objects.all().name:

this is the error. It should be value in not type in

In PyCharm it underlines type in this line very clearly. You should try it.

Welcome @ock12 !

Side Note: When posting code, templates, error messages and tracebacks here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of editing your original post for this, please remember to do this in the future.)

In addition to the previous response, this expression:

is not going to do what I think you want it to do.

If you’re trying to create a list of the name field from all instances of TypeConge, this won’t do it. See the docs and examples for the values_list function.

Hello again,

@KenWhitesell thanks, i changed the elif to

elif value in TypeConge.objects.values_list('name'):
        type = value.capitalize()

it works,
i did used value_list method before but it skipped my mind today.

@boxed thanks for the reply

Good day

This works, but it’s horrible for performance. Instead of asking the DB “is x in there?” you are asking “give me all of them” and then checking if it’s in a list inside python.

You want:

elif TypeConge.objects.filter(name=value).exists():

this is one clean small select statement.

Hello @boxed ,
Yes, the line works as well.

elif TypeConge.objects.filter(name=value).exists():

Thanks,