简体   繁体   English

/profiles/user-profile/2/ 'int' 处的 AttributeError object 没有属性 '_meta'

[英]AttributeError at /profiles/user-profile/2/ 'int' object has no attribute '_meta'

Here I am using an api of notifications.在这里,我使用了 api 的通知。 here is the reference of that api link .这是 api链接的参考。 I have follow the instructions given on the github read me file.我已按照 github 自述文件中的说明进行操作。 But when i try to send an email by actor to recipient.it shows me the error 'int' object has no attribute '_meta' .但是,当我尝试通过演员向收件人发送 email 时,它显示错误“int”object 没有属性“_meta” The actor field and recipient both accept user_ids and I have put them manually. actor 字段和 recipient 都接受 user_ids,我已经手动放置了它们。 But still didn't work.但仍然没有用。

profiles/Views.py配置文件/Views.py

class UserProfileFollowToggle(LoginRequiredMixin,View):
    login_url = '/accounts/login/'
    def post(self, request, *args, **kwargs):
            user_to_toggle_pk=kwargs.get('pk')
            username_to_toggle = request.POST.get("username")
            profile_, is_following = UserProfile.objects.toggle_follow(request.user, request.user.id, user_to_toggle_pk ,username_to_toggle)
            return redirect(f'/profiles/{username_to_toggle}')

profiles/models.py配置文件/models.py

class ProfileManager(models.Manager):
    def toggle_follow(self, request_user,user_id,user_to_toggle_pk, username_to_toggle):
        profile_ = UserProfile.objects.get(user__username__iexact=request_user.username)
        is_following = False
        follower = profile_.follower.filter(username__iexact=username_to_toggle).first()
        if follower:
            profile_.follower.remove(follower.id)
            notify.send(user_id, recipient=user_to_toggle_pk, verb='unfollow you')
        else:
            new_follower = User.objects.get(username__iexact=username_to_toggle)
            profile_.follower.add(new_follower.id)
            notify.send(user_id, recipient=user_to_toggle_pk, verb='follow you')
            is_following = True
        return profile_, is_following

traceback:追溯:

Traceback (most recent call last):
  File "C:\Users\AHMED\anaconda3\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\AHMED\anaconda3\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\AHMED\anaconda3\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\AHMED\anaconda3\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\AHMED\anaconda3\lib\site-packages\django\contrib\auth\mixins.py", line 52, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "C:\Users\AHMED\anaconda3\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\AHMED\grapPub\grabpublic\profiles\views.py", line 30, in post
    profile_, is_following = UserProfile.objects.toggle_follow(request.user, request.user.id, user_to_toggle_pk ,username_to_toggle)
  File "C:\Users\AHMED\grapPub\grabpublic\profiles\models.py", line 22, in toggle_follow
    notify.send(user_id, recipient=user_to_toggle_pk, verb='unfollow you')
  File "C:\Users\AHMED\anaconda3\lib\site-packages\django\dispatch\dispatcher.py", line 173, in send
    return [
  File "C:\Users\AHMED\anaconda3\lib\site-packages\django\dispatch\dispatcher.py", line 174, in <listcomp>
    (receiver, receiver(signal=self, sender=sender, **named))
  File "C:\Users\AHMED\grapPub\grabpublic\notifications\base\models.py", line 288, in notify_handler
    actor_content_type=ContentType.objects.get_for_model(actor),
  File "C:\Users\AHMED\anaconda3\lib\site-packages\django\contrib\contenttypes\models.py", line 40, in get_for_model
    opts = self._get_opts(model, for_concrete_model)
  File "C:\Users\AHMED\anaconda3\lib\site-packages\django\contrib\contenttypes\models.py", line 27, in _get_opts
    model = model._meta.concrete_model

Exception Type: AttributeError at /profiles/user-profile/2/
Exception Value: 'int' object has no attribute '_meta'

If more information is required than tell me in a comment section.如果需要更多信息,请在评论部分告诉我。 I will update my question with that information.我会用这些信息更新我的问题。

I think the AttributeError is caused, because you are trying to pass a user pk to the send function我认为 AttributeError 是造成的,因为你正试图将用户 pk 传递给发送 function

notify.send(user_id, recipient=user_to_toggle_pk, verb='unfollow you')
                                ^^^^^^^^^^^^^^^^     

but the send function expect an User object. So try something like this..但是发送 function 期望用户 object。所以尝试这样的事情..

user = User.objects.get(pk=user_to_toggle_pk)
notify.send(user_id, recipient=user, verb='unfollow you')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DoesNotExist at /profiles/user-profile/ UserProfile 匹配查询不存在 - DoesNotExist at /profiles/user-profile/ UserProfile matching query does not exist AttributeError: &#39;User_Profile&#39; 对象没有属性 &#39;__name__&#39; - AttributeError: 'User_Profile' object has no attribute '__name__' 显示用户个人资料出现错误:“ AnonymousUser”对象没有属性“ _meta” - Display User Profile getting ERROR: 'AnonymousUser' object has no attribute '_meta' AttributeError:int对象没有属性 - AttributeError : int object has no attribute AttributeError:&#39;int&#39;对象没有用户输入的属性&#39;isdigit&#39; - AttributeError: 'int' object has no attribute 'isdigit' from user input AttributeError : &#39;NoneType&#39; 对象没有属性 &#39;_meta&#39; - AttributeError : 'NoneType' object has no attribute '_meta' AttributeError:&#39;dict&#39;对象没有属性&#39;_meta&#39; - AttributeError: 'dict' object has no attribute '_meta' AttributeError:类型对象“ MyModel”没有属性“ _meta” - AttributeError: type object 'MyModel' has no attribute '_meta' / &#39;AnonymousUser&#39; 对象的 AttributeError 没有属性 &#39;_meta&#39; - AttributeError at / 'AnonymousUser' object has no attribute '_meta' AttributeError:类型 object 'StaffFilter' 没有属性 '_meta' - AttributeError: type object 'StaffFilter' has no attribute '_meta'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM