2

I am trying to create a follower system where user can get to follow investor not between user and user but between user and investor. However I am getting an error.

I am still not sure if i have to create a different table for follow or not but here is the code. Please share your idea for better modelling of follow system between user and investor

from 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('/')

UPDATE The User is from django.contrib.auth.models

stack trace

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
2
  • 1
    Add the definition of User model and your stack trace for the error. Commented Oct 9, 2017 at 10:42
  • @ShikharChauhan I have updated the question with stack trace. Regarding User models it is inherited from the django.contrib.auth.models Commented Oct 9, 2017 at 14:07

1 Answer 1

1

To answer your question, change:

investor, created = cls.objects.get_or_create(investor=investor)

into

investor, created = cls.objects.get_or_create(investor=investor.investor)

You should rename investor field into user to avoid confusion ;)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot this solved my problem. I got confused due to naming issue. I feel problem in giving the right name :). Do you suggest any idea on modelling part? I think a separate table will be better for follow because I might need follow time, unfollow time etc. What you say?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.