Jun. 16, 2020
halted due to a technical problem:
>>> obj.likes.add(me)Traceback (most recent call last):File "<console>", line 1, in <module>File "/root/.local/share/virtualenvs/reactjs-_0JUQ8jY/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 921, in add(opts.app_label, opts.object_name)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。
update django of the latest version:
python -m pip install -U Django
update both python3 (https://stackoverflow.com/questions/55674176/django-cant-find-new-sqlite-version-sqlite-3-8-3-or-later-is-required-found)
# python3cd ~wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xztar xJf Python-3.7.3.tar.xzcd Python-3.7.3./configuremake && make install
add new path into ~/.bash_profile and comment other python related paths to let python3 apply this 3.7.3 version.
export PATH=$HOME/opt/python-3.7.3/bin:$PATH# export PATH=/root/.pyenv/versions/3.7.2/bin:$PATH# ...
then
source ~/.bash_profile
close the terminal and login again to make it permanent.
update sqlite3 (https://stackoverflow.com/questions/55674176/django-cant-find-new-sqlite-version-sqlite-3-8-3-or-later-is-required-found)
# sqlite3cd ~wget https://www.sqlite.org/2019/sqlite-autoconf-3290000.tar.gztar zxvf sqlite-autoconf-3290000.tar.gzcd sqlite-autoconf-3290000./configure --prefix=$HOME/opt/sqlitemake && make install
add new paths into ~/.bash_profile to make sqlite3 only refer to python3 instead of python(2).
export PATH=$HOME/opt/sqlite/bin:$PATHexport LD_LIBRARY_PATH=$HOME/opt/sqlite/libexport LD_RUN_PATH=$HOME/opt/sqlite/lib
then
source ~/.bash_profile
close the terminal and login again to make it permanent.
check the current version
[root@localhost Twittme]# python3 --versionPython 3.7.3[root@localhost Twittme]# sqlite3 --version3.29.0 2019-07-10 17:32:03 fc82b73eaac8b36950e527f12c4b5dc1e147e6f4ad2217ae43ad82882a88bfa6[root@localhost Twittme]# python3 -m django --version3.0.7
re-install rest framework
pipenv install djangorestframework
- get back to M2M work before.
so after the upgrade we can apply>>> from tweets.models import Tweet, TweetLike>>> from django.contrib.auth import get_user_model>>> obj = Tweet.objects.first()>>> User = get_user_model()>>> me = User.objects.first() # the first user of the query set>>> obj.likes.add(me)>>> obj.likes.all()<QuerySet [<User: root>]>>>> obj.likes.remove(me)>>> obj.likes.all()<QuerySet []>>>> qs = User.objects.all() # query set of all users>>> obj.likes.set(qs)>>> obj.likes.all()<QuerySet [<User: root>]>>>> TweetLike.objects.all()<QuerySet [<TweetLike: TweetLike object (5)>]>>>> TweetLike.objects.first().timestampdatetime.datetime(2020, 6, 18, 17, 8, 1, 965393, tzinfo=<UTC>)>>> obj.likes.remove(me)>>> obj.likes.all()<QuerySet []>>>> TweetLike.objects.create(user=me, tweet=obj) # create a TweetLike object manually<TweetLike: TweetLike object (6)>>>> obj.likes.all()<QuerySet [<User: root>]>^>>> obj.likes.add(me)>>> obj.likes.all()<QuerySet [<User: root>]>>>> empty_users = User.objects.none() # a query set of no users>>> empty_users<QuerySet []>>>> obj.likes.set(empty_users) # set the query set into none>>> obj.likes.all()<QuerySet []>>>>
>>> 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.
