如何在Django中通过电子邮件过滤获取用户对象

时间:2019-05-01 17:34:00

标签: python django python-3.x django-models django-views

我正在尝试使用默认用户模型通过电子邮件过滤来获取Django中的特定用户。我在views.py中正在执行以下操作,但无法正常工作:

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)

但它会跟随错误

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'

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

您可以使用exact匹配从模型中检索对象。

例如:

user = User.objects.get(email__exact='usermail@example.com')

这将生成如下所示的SQL:

SELECT ... WHERE email = 'usermail@example.com';