-1

I'm trying to get a string as a variable to represent my column in DB.

l_id =1
level=7
test = "level"+level
device_level_mapping = DeviceLevelMapping.objects.filter(test = l_id)

It says keyword cant be an expression.

I need to do something like this. In PHP we do like this.

$a = "test"+"1";
$$a = 10;
echo $test1; // now test1 has become a variable.

How do I make this happen in python?

Praful Bagai
  • 16,684
  • 50
  • 136
  • 267
  • 2
    Is this even legal code? `'level'+7` should fail. Also, I see no exceptions that you are trying to raise. I suspect you have posted the wrong section of code – inspectorG4dget Oct 30 '13 at 06:57
  • *expression. Sorry typo. – Praful Bagai Oct 30 '13 at 06:59
  • Still not legal code. You have one extra `)` in the third line. Also, what is the `= l_id` doing there. Also, what is the value of `l_id`? – inspectorG4dget Oct 30 '13 at 07:02
  • In my opinion the real problem is: The DB layout is wrong and now you're going to work around it by even uglier code. – Matthias Oct 30 '13 at 07:03
  • @inspectorG4dget I gave both of them in the answer, I dont know why I am still downvoted. :( – thefourtheye Oct 30 '13 at 07:04
  • @Matthias- Any suggestions for this question please. – Praful Bagai Oct 30 '13 at 07:06
  • @thefourtheye: I didn't downvote you. IMHO the question is far too underspecified for me to be able to provide/comprehend an answer. Therefore, I cannot vote on any answer. I daresay you didn't deserve the downvote, though. Also, it seems that OP wants to troll on us (see previous comment at me). I wonder if this is the `mod_wsgi` troll we saw a while back – inspectorG4dget Oct 30 '13 at 07:07
  • 2
    @user1162512 Be nice to people. inspectorG4dget points out what is wrong with your question. Please fix your question first. – thefourtheye Oct 30 '13 at 07:08
  • @user1162512: it is up to you, as the OP, to provide valid code, from which we can reproduce your errors, so that we can help you (see [SSCCE](http://sscce.org/) for more. – inspectorG4dget Oct 30 '13 at 07:10
  • I've updated the code. Please check. – Praful Bagai Oct 30 '13 at 07:12
  • 1
    First of all: Are we talking about Django here? – 0x434D53 Oct 30 '13 at 07:14
  • @tripleee - I have already gone through this thread and I didnt got the expected o/p. Any more suggestions please with a demo code. Thanks – Praful Bagai Oct 30 '13 at 07:21

1 Answers1

1
test = "level"+level
d = {test: l_id}
device_level_mapping = DeviceLevelMapping.objects.filter(**d)
Naster
  • 344
  • 1
  • 7