Trước tiên, bạn nên hiểu cách store additional information about users. Nó đòi hỏi một mô hình khác có quan hệ với một người dùng, mô hình "hồ sơ".
Sau đó, bạn có thể sử dụng một trường M2M, giả sử bạn muốn sử dụng django-annoying, bạn có thể xác định mô hình hồ sơ người dùng của bạn như vậy:
from django.db import models
from annoying.fields import AutoOneToOneField
class UserProfile(models.Model):
user = AutoOneToOneField('auth.user')
follows = models.ManyToManyField('UserProfile', related_name='followed_by')
def __unicode__(self):
return self.user.username
Và sử dụng nó như vậy:
In [1]: tim, c = User.objects.get_or_create(username='tim')
In [2]: chris, c = User.objects.get_or_create(username='chris')
In [3]: tim.userprofile.follows.add(chris.userprofile) # chris follows tim
In [4]: tim.userprofile.follows.all() # list of userprofiles of users that tim follows
Out[4]: [<UserProfile: chris>]
In [5]: chris.userprofile.followed_by.all() # list of userprofiles of users that follow chris
Out[5]: [<UserProfile: tim>]
Ngoài ra, lưu ý rằng bạn có thể kiểm tra/sử dụng lại các ứng dụng như django-subscription, django-actstream, django-social (khó sử dụng hơn) ...
Y Bạn có thể muốn xem các gói django cho notifications và activities vì tất cả chúng đều yêu cầu một số thiết kế cơ sở dữ liệu theo dõi/đăng ký.
Trừ khi bạn có câu hỏi liên quan đến lập trình cụ thể, điều này phù hợp hơn với tư cách là bài đăng trong diễn đàn. – patrickn
@patrickn có lẽ là có nhưng nó cũng có thể được xem như một câu hỏi. Vì tôi không có đầu mối nên tôi bắt đầu hỏi về nó. –