Django contenttypes follow/unfollow
Попытка использовать contenttypes для хранения следующих отношений пользователей.
Я могу создать слежку, но не могу удалить слежку из базы данных.
Код модели
accounts app
------------
from activities.models import Activity
class CustomUser(AbstractUser):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
follow = GenericRelation(Activity)
activities app
--------------
class Activity(models.Model)
FOLLOW = 'F'
LIKE = 'L'
ACTIVITY_TYPES = [
(FOLLOW, 'Follow'),
(LIKE, 'Like'),
]
## stores the user_id of the Liker/Follower
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
## stores 'F' or 'L'
activity_type = models.CharField(max_length=2, choices=ACTIVITY_TYPES)
date = models.ForeignKey(ContentType, on_delete=models.CASCADE)
## stores CustomUser id of the user who is followed or the id of the post that is liked
object = models.UUIDField(null=True, blank=True, db_index=True)
content_object = GenericForeignKey('content_type', 'object_id')
class Meta:
indexes = [models.Index(fields=["content_type", "object_id"]),]
View Code
follower = User.objects.get(id=follower_id)
followed = User.objects.get(id=followed_id)
## This is the contenttypes model
a = Activity.objects.filter(user=follower.id, object_id=followed.id)
## This is my janky way of checking whether the follower is already following the followed
if len(a) == 0:
## This works to create a follow
followed.follow.create(activity_type=Activity.FOLLOW, user=follower)
Я подумал, что это может сработать, чтобы удалить follow
followed.follow.remove(a)
но он выдает ошибку 'Объект QuerySet' не имеет атрибута 'pk'