【问题标题】:django ajax: User matching query does not existdjango ajax:用户匹配查询不存在
【发布时间】:2020-08-11 15:47:18
【问题描述】:

所以我做了一个 django ajax,每次有新消息时,我添加到我的用户模型的通知对象都会是 +1,问题是我不知道为什么会给我这个错误:

django.contrib.auth.models.User.DoesNotExist: User matching query does not exist.

这是我的代码:

views.py:

def count_notification(request): 
    user = request.user  
    obj = user.profile.objects.get(pk=1)
    obj.notifications = obj.notifications +1
    obj.save()
    response_data = {'success':1}
    return HttpResponse(json.dumps(response_data), content_type="application/json")

urls.py:

path('messages/notification/', count_notification)

html文件js

// Add the notification val
$.get('notification/')

models.py:

class ProfileImage(models.Model):
    """
    Profile model
    """
    user = models.OneToOneField(
        verbose_name=_('User'),
        to=settings.AUTH_USER_MODEL,
        related_name='profile',
        on_delete=models.CASCADE
    )
    avatar = models.ImageField(upload_to='profile_image')
    notifications = models.FloatField(default='0')

谢谢你的帮助

【问题讨论】:

  • 它告诉你用户不存在,所以在这种情况下,id 等于1User 实例不存在,因此查询失败。
  • @lmiguelvargasf 那么我该如何修复它,我想更改为已登录用户的通知值

标签: javascript python django ajax


【解决方案1】:
obj = user.profile.objects.get(pk=1)

我认为您的应用正在处理许多用户,每个用户都有自己的个人资料和自己的通知计数,所以为什么要将 id / pk 固定为 1,您需要获取相关的个人资料,而不是硬编码 id

试试下面的代码

def count_notification(request):
    user = request.user

    user.profile.notifications += 1
    user.profile.save()

    response_data = {'success':1}

    return HttpResponse(json.dumps(response_data), content_type="application/json")

改变

notifications = models.FloatField(default='0')

notifications = models.IntegerField(default='0')

它确实更有意义,因为通知计数是一个整数而不是浮点数,并且不要忘记重新运行迁移

更新

我猜你还没有在settings.py 中定义自定义User 模型,在这种情况下你需要在models.py 中进行一些更改

参考https://docs.djangoproject.com/en/3.0/topics/auth/customizing/#reusable-apps-and-auth-user-model

from django.contrib.auth import get_user_model  # get the default User model

User = get_user_model()

class ProfileImage(models.Model):
    """
    Profile model
    """
    user = models.OneToOneField(
        verbose_name=_('User'),
        # to=settings.AUTH_USER_MODEL, # you don't need this since you didn't 
                                       # defined a custom User model in 
                                       # setting.py
        to = User,  # HERE
        related_name='profile',
        on_delete=models.CASCADE
    )
    avatar = models.ImageField(upload_to='profile_image')
    notifications = models.IntegerField(default='0')  # change the field type 

更新 2

from django.contrib.auth import get_user_model

User = get_user_model()

..

def count_notification(request):
    # user = request.user
    user = User.objects.filter(id=request.user.id).first()  # HERE

    user.profile.notifications += 1
    user.profile.save()

    response_data = {'success':1}

    return HttpResponse(json.dumps(response_data), content_type="application/json")

【讨论】:

  • settings.py请先检查AUTH_USER_MODEL你提到你的自定义User模型
  • 我的设置中没有 auth_user_model
  • 我也做迁移
猜你喜欢
  • 2015-03-13
  • 2021-07-19
  • 1970-01-01
  • 2020-12-01
  • 1970-01-01
  • 2014-09-22
  • 1970-01-01
  • 1970-01-01
  • 2015-01-28
相关资源
最近更新 更多