I have a model, called cards, which contains a number of elements I want to loop through. So I was trying to use some dictionary comprehension as follows:
cards = Card.objects.filter(device=device)
output = { c.id : [c.generateData(), c.sensor.getLatestTime()] for c in cards}
While running that code, it breaks on the output statement with the following error:
The QuerySet value for an exact lookup must be limited to one result using slicing.
Most of the pre-existing answers I found for that exact error were for cases where the queryset is being confused with a single field, as in 1, 2. However in my case, I am expecting, and handling it as a queryset by looping through it.
I have also seen a question that is similar in here about using dictionary comprehension on the models, however as far as I can see, my format is almost the same (Unless the list as a second parameter is causing an issue somehow?)
Edit: I noticed while trying to test where the error is occuring, that even a simple for-loop such as:
for card in cards:
is giving the same error, the error seems to be somehow in the line:
cards = Card.objects.filter(device=device)
Which honestly isn't making much sense to me. It feels as if I am forgetting something fundemental but I can't see it yet.
Found out the error was very dumb, in the statement:
output = { c.id : [c.generateData(), c.sensor.getLatestTime()] for c in cards}
The error was occuring around here, causing me to think that the issue occured at:
cards = Card.objects.filter(device=device)
Since the code broke around that part, however, looking further into it, since replacing this statement with:
cards = Card.objects.all()
Solved the issue, I found out that the error was occurring on the filter parameters, since I was doing the following a few lines earlier:
device = Device.objects.filter(project__user=user,id=id)
thus, tried to use a query-set in the 2nd condition. Thus replacing it with:
device = Device.objects.get(project__user=user,id=id)
resolved my issue.