Issue
I am trying to get a specific user in Django via email filtering using default user model. I am doing the following in my views.py but it is not working:
def shareCode(request):
if request.method=="POST":
email = request.POST.get("mail")
print(email)
id = int(request.POST.get('id'))
user = User.objects.get(email = email)
obj = Code.objects.get(pk=id,user = user.id)
res = {"shared":"fasle"}
obj2 = Code(name=obj.name,code=obj.code,shared=True)
obj2.save()
res = {
"shared":True
}
return HttpResponse(json.dumps(res))
models.py
class Code(models.Model):
name = models.CharField(max_length=255)
code = models.TextField()
user = models.ForeignKey(User,on_delete=models.CASCADE)
shared = models.BooleanField(default=False)
but its getting be following error
Internal Server Error: /editor/share
Traceback (most recent call last):
File "D:\stocksapp\winenv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "D:\stocksapp\winenv\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "D:\stocksapp\winenv\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\stocksapp\main\views.py", line 138, in shareCode
obj = Code.objects.get(id=id,user = user)
File "D:\stocksapp\winenv\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "D:\stocksapp\winenv\lib\site-packages\django\db\models\query.py", line 390, in get
clone = self.filter(*args, **kwargs)
File "D:\stocksapp\winenv\lib\site-packages\django\db\models\query.py", line 844, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "D:\stocksapp\winenv\lib\site-packages\django\db\models\query.py", line 862, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "D:\stocksapp\winenv\lib\site-packages\django\db\models\sql\query.py", line 1263, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "D:\stocksapp\winenv\lib\site-packages\django\db\models\sql\query.py", line 1287, in _add_q
split_subq=split_subq,
File "D:\stocksapp\winenv\lib\site-packages\django\db\models\sql\query.py", line 1225, in build_filter
condition = self.build_lookup(lookups, col, value)
File "D:\stocksapp\winenv\lib\site-packages\django\db\models\sql\query.py", line 1096, in build_lookup
lookup = lookup_class(lhs, rhs)
File "D:\stocksapp\winenv\lib\site-packages\django\db\models\lookups.py", line 20, in __init__
self.rhs = self.get_prep_lookup()
File "D:\stocksapp\winenv\lib\site-packages\django\db\models\lookups.py", line 70, in get_prep_lookup
return self.lhs.output_field.get_prep_value(self.rhs)
File "D:\stocksapp\winenv\lib\site-packages\django\db\models\fields\__init__.py", line 965, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'code'
Any help will be appreciated
Solution
You can retrieve an object from models using exact
match.
e.g:
user = User.objects.get(email__exact='[email protected]')
This would generate SQL that looks like this:
SELECT ... WHERE email = '[email protected]';
Answered By - Ratidzo G
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.