如何解决Django Tweepy API.friends澄清
我有一个应用程序,它可以从登录用户那里获取朋友和关注者,然后将其提供给html表。我用以下代码检查了既未更新又未删除的条目。
# Setup Twitter API instance of logged User
api = get_api(user)
me = api.me()
# Fetch logged User's friends
user_friends = api.friends(me.screen_name,count=200)
# Fetch logged User's followers
user_followers = api.followers(me.screen_name,count=200)
# Check for excess entries and delete them if any
existing_contacts = Contact.objects.filter(user=user)
for contact in existing_contacts:
if contact not in user_friends and contact not in user_followers:
contact.delete()
我决定使用游标方法以提供可伸缩性,但是更改后,当前代码将删除所有联系人。可能是因为它在任何列表中都找不到该对象。有什么想法吗?
# Setup Twitter API instance of logged User
api = get_api(user)
me = api.me()
# Fetch logged User's friends
user_friends = tweepy.Cursor(api.friends,me.screen_name,count=200).items()
# Fetch logged User's followers
user_followers = tweepy.Cursor(api.followers,count=200).items()
# Check for excess entries and delete them if any
existing_contacts = Contact.objects.filter(user=user)
for contact in existing_contacts:
if contact not in user_friends and contact not in user_followers:
contact.delete()
models.py
class Contact(models.Model):
twitter_id = models.CharField(max_length=30)
profile_image_url = models.CharField(max_length=200)
profile_background_image_url = models.CharField(max_length=200,default='')
screen_name = models.CharField(max_length=50)
name = models.CharField(max_length=200,default='')
favourites_count = models.BigIntegerField(default=0)
followers_count = models.BigIntegerField(default=0)
friends_count = models.BigIntegerField(default=0)
statuses_count = models.BigIntegerField(default=0)
description = models.CharField(max_length=400,default='')
location = models.CharField(max_length=200,default='')
friendship_status = models.IntegerField(default=0)
user = models.ForeignKey(User,on_delete=models.CASCADE)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。