Jun. 16, 2020

halted due to a technical problem:

  1. >>> obj.likes.add(me)
  2. Traceback (most recent call last):
  3. File "<console>", line 1, in <module>
  4. File "/root/.local/share/virtualenvs/reactjs-_0JUQ8jY/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 921, in add
  5. (opts.app_label, opts.object_name)
  6. AttributeError: Cannot use add() on a ManyToManyField which specifies an intermediary model. Use tweets.TweetLike's Manager instead.

django version before 2.2 does not support add or set to ManyToManyField.
Also, it’s not very clear that how to install corresponding sqlite 3.8.3
Now look for solving methods and focus on organizing algorithms.

Jun. 17, 2020

(continue)
solved the problem of django version updating problem。

  1. update django of the latest version:

    1. python -m pip install -U Django
  2. update both python3 (https://stackoverflow.com/questions/55674176/django-cant-find-new-sqlite-version-sqlite-3-8-3-or-later-is-required-found)

    1. # python3
    2. cd ~
    3. wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz
    4. tar xJf Python-3.7.3.tar.xz
    5. cd Python-3.7.3
    6. ./configure
    7. make && make install

    add new path into ~/.bash_profile and comment other python related paths to let python3 apply this 3.7.3 version.

    1. export PATH=$HOME/opt/python-3.7.3/bin:$PATH
    2. # export PATH=/root/.pyenv/versions/3.7.2/bin:$PATH
    3. # ...

    then

    1. source ~/.bash_profile

    close the terminal and login again to make it permanent.

  3. update sqlite3 (https://stackoverflow.com/questions/55674176/django-cant-find-new-sqlite-version-sqlite-3-8-3-or-later-is-required-found)

    1. # sqlite3
    2. cd ~
    3. wget https://www.sqlite.org/2019/sqlite-autoconf-3290000.tar.gz
    4. tar zxvf sqlite-autoconf-3290000.tar.gz
    5. cd sqlite-autoconf-3290000
    6. ./configure --prefix=$HOME/opt/sqlite
    7. make && make install

    add new paths into ~/.bash_profile to make sqlite3 only refer to python3 instead of python(2).

    1. export PATH=$HOME/opt/sqlite/bin:$PATH
    2. export LD_LIBRARY_PATH=$HOME/opt/sqlite/lib
    3. export LD_RUN_PATH=$HOME/opt/sqlite/lib

    then

    1. source ~/.bash_profile

    close the terminal and login again to make it permanent.

  4. check the current version

    1. [root@localhost Twittme]# python3 --version
    2. Python 3.7.3
    3. [root@localhost Twittme]# sqlite3 --version
    4. 3.29.0 2019-07-10 17:32:03 fc82b73eaac8b36950e527f12c4b5dc1e147e6f4ad2217ae43ad82882a88bfa6
    5. [root@localhost Twittme]# python3 -m django --version
    6. 3.0.7
  5. re-install rest framework

    1. pipenv install djangorestframework
  • get back to M2M work before.
    1. >>> from tweets.models import Tweet, TweetLike
    2. >>> from django.contrib.auth import get_user_model
    3. >>> obj = Tweet.objects.first()
    4. >>> User = get_user_model()
    5. >>> me = User.objects.first() # the first user of the query set
    6. >>> obj.likes.add(me)
    7. >>> obj.likes.all()
    8. <QuerySet [<User: root>]>
    9. >>> obj.likes.remove(me)
    10. >>> obj.likes.all()
    11. <QuerySet []>
    12. >>> qs = User.objects.all() # query set of all users
    13. >>> obj.likes.set(qs)
    14. >>> obj.likes.all()
    15. <QuerySet [<User: root>]>
    16. >>> TweetLike.objects.all()
    17. <QuerySet [<TweetLike: TweetLike object (5)>]>
    18. >>> TweetLike.objects.first().timestamp
    19. datetime.datetime(2020, 6, 18, 17, 8, 1, 965393, tzinfo=<UTC>)
    20. >>> obj.likes.remove(me)
    21. >>> obj.likes.all()
    22. <QuerySet []>
    23. >>> TweetLike.objects.create(user=me, tweet=obj) # create a TweetLike object manually
    24. <TweetLike: TweetLike object (6)>
    25. >>> obj.likes.all()
    26. <QuerySet [<User: root>]>
    27. ^
    28. >>> obj.likes.add(me)
    29. >>> obj.likes.all()
    30. <QuerySet [<User: root>]>
    31. >>> empty_users = User.objects.none() # a query set of no users
    32. >>> empty_users
    33. <QuerySet []>
    34. >>> obj.likes.set(empty_users) # set the query set into none
    35. >>> obj.likes.all()
    36. <QuerySet []>
    37. >>>
    so after the upgrade we can apply
    >>> obj.likes.add() # add an element
    >>> obj.likes.remove() # remove an element
    >>> obj.likes.set() # set a query set
    >>> TweetLike.objects(user=, tweet=) # manually create an object

Let’s apply those methods in our projects.