This is my code:
PostModels = {
'dPost': DPost,
'sPost': SPost,
'rPost': RPostModel,
}
for postType, Post in PostModels.items():
try:
post = Post.objects.get(pk=pk)
except:
pass
else:
return RPostModel.objects.filter(postType=post)
When I do post = Post.objects.get(pk=pk)
, Post
is the variable Post
in the for loop. So on first loop, it searches the objects in DPost
, on the next loop it searchs SPost
etc. However, when it reaches the return statement, it gives an error saying:
django.core.exceptions.FieldError:
Cannot resolve keyword 'postType' into field.
Choices are: createdAt, dPost, dPost_id, sPost, sPost_id, rPost, rPost_id
How do I make postType
equal to what the postType
variable is equal to? In other words, in the loop, how can I get my return statement to be these statements:
return RPostModel.objects.filter(dPost=post)
return RPostModel.objects.filter(sPost=post)
return RPostModel.objects.filter(rPost=post)
rather than this:
return RPostModel.objects.filter(postType=post)