我正在尝试创建一个关注者系统,用户可以在其中关注投资者,而不是在用户与用户之间,而是在用户与投资者之间。但是我收到一个错误。
我仍然不确定我是否必须为跟随创建一个不同的表,但这是代码。请分享您的想法,以便更好地建模用户和投资者之间的跟随系统
来自 models.py:
class Investor(models.Model):
investor = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=200, blank=False,
null=False, help_text='Full Name')
followers = models.ManyToManyField(
User, related_name='followers', blank=True)
@classmethod
def follow_investor(cls, investor, follower):
investor, created = cls.objects.get_or_create(investor=investor)
investor.followers.add(follower)
@classmethod
def unfollow_investor(cls, investor, follower):
investor, created = cls.objects.get_or_create(investor=investor)
investor.followers.remove(follower)
url(r'^investor/(?P<action>.+)/(?P<slug>[-\w]+)$',
views.follow_unfollow_investor, name="follow-unfollow-investor"),
def follow_unfollow_investor(request, action, slug=None):
follower = request.user
investor = Investor.objects.get(slug=slug)
if action == "add":
Investor.follow_investor(investor, follower)
elif action == "remove":
Investor.unfollow_investor(user, follower)
return redirect('/')
更新用户来自 django.contrib.auth.models
堆栈跟踪
Internal Server Error: /investor/add/milan-raymond
Traceback (most recent call last):
File "/home/milan/.virtualenvs/startup/lib/python3.5/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/milan/.virtualenvs/startup/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/milan/.virtualenvs/startup/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/milan/Projects/startup/investors/views.py", line 32, in follow_unfollow_investor
Investor.follow_investor(investor, follower)
File "/home/milan/Projects/startup/investors/models.py", line 72, in follow_investor
investor, created = cls.objects.get_or_create(investor=investor)
File "/home/milan/.virtualenvs/startup/lib/python3.5/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/milan/.virtualenvs/startup/lib/python3.5/site-packages/django/db/models/query.py", line 464, in get_or_create
return self.get(**lookup), False
File "/home/milan/.virtualenvs/startup/lib/python3.5/site-packages/django/db/models/query.py", line 371, in get
clone = self.filter(*args, **kwargs)
File "/home/milan/.virtualenvs/startup/lib/python3.5/site-packages/django/db/models/query.py", line 784, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/home/milan/.virtualenvs/startup/lib/python3.5/site-packages/django/db/models/query.py", line 802, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/home/milan/.virtualenvs/startup/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1250, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/home/milan/.virtualenvs/startup/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1276, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "/home/milan/.virtualenvs/startup/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1179, in build_filter
self.check_related_objects(field, value, opts)
File "/home/milan/.virtualenvs/startup/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1076, in check_related_objects
self.check_query_object_type(value, opts, field)
File "/home/milan/.virtualenvs/startup/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1054, in check_query_object_type
(value, opts.object_name))
ValueError: Cannot query "milan": Must be "User" instance.
[09/Oct/2017 14:02:18] "GET /investor/add/milan-raymond HTTP/1.1" 500 138655
我是一名优秀的程序员,十分优秀!