Specifying an alias to column names in Django -
i querying auth_users table of django return users matched search criteria.
users = user.objects.filter( q(first_name__icontains = name)| q(username__icontains = name) | q(email__icontains = name) | q(last_name__icontains = name) ).values( 'id', 'username', 'first_name', 'last_name', 'email' ).order_by('first_name')
i wondering if it's possible me change name of 'first_name' 'firstname'? can in sql [select first_name firstname auth_users
];
so can access using firstname instead of first_name
thanks
i create translation layer between 2 namespaces. since using django orm , javascript code legacy assume want in python.
def translate(js_column_name): return { 'firstname': 'first_name', }.get(js_column_name, 'string return when not found')
using translation function create keyword arguments in dictionary , pass q
function using **
:
user.objects.filter( q(**{ translate(js_firstname) + '_icontains': name }) ...
of course assume not know input translating 'firstname'
otherwise define correct column names without needing function (i.e. create query normal). instead assume receiving javascript column names dynamically in case approach use.
p.s. hope best answers question. in future advise provide context problem (i.e. specify have problem because..., , javascript names appear dynamically etc.). way question answered more quickly.
Comments
Post a Comment