Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't query from django where field name has double underscore

Can't query from django where field name has double underscore. This is because in the django query language __ has its own meaning

so how can I query a field whose actual name is "my__fyeild__name" ?

template.fields.filter(my__fyeild__name="aaa")

In the database the column has a name with two underscores __, and I am not allowed to rename that column.

like image 427
dov.amir Avatar asked Dec 18 '22 15:12

dov.amir


1 Answers

You can't, since Django will always split on the __ part. But I think you do not need this anyway. You can define a field at the Django level, that has a different name on the database level.

We can define a model for example with a field where we specify the name of the column at the database level with the db_column=... parameter [Django-doc]:

class SomeModel(models.Model):
    my_field_name = models.CharField(max_length=128, db_column='my__field__name')

So here you can query on my_field_name, and Django will automatically use the my__field__name in the query.

like image 182
Willem Van Onsem Avatar answered Jan 22 '23 15:01

Willem Van Onsem