Мир вам! В настоящее время я работаю над своим первым django проектом, конечно же, как и все django разработчики, я сталкивался с таким количеством ошибок, моя текущая проблема заключается в том, что я хочу настроить систему комментариев (DB, front-end, back-end), я искал решение для этого, но ответа не найдено. это мои модели:
from django.db import models
from datetime import datetime
from django.contrib.auth.models import User
class Comment(models.Model):
# if I am a reply, so "comment" my parent comment, so each comment will have replies_set
comment = models.ForeignKey('self', on_delete=models.CASCADE, default = '')
user = models.ForeignKey(User, on_delete=models.CASCADE, default='')
value = models.CharField(max_length=100, default='')
date = models.DateField(default = datetime.now())
def __str__(self):
return self.value
class Reaction(models.Model):
comment = models.ForeignKey(Comment, on_delete=models.CASCADE, default = '')
# the user that this love belong to
user = models.ForeignKey(User, on_delete=models.CASCADE, default='')
# type: like:0, love:1, laugh:2, cry:3, angry:4, ...
TYPES = [
(0, 'like'),
(1, 'love'),
(2, 'laugh'),
(3, 'cry'),
(4, 'angry'),
]
reaction_type = models.IntegerField(choices=TYPES, default=0)
def __str__(self):
return self.types[self.reaction_type][1]
Я хочу сделать что-то похожее на твиттер-комментарии, чтобы навсегда ответить не на такую вещь, как в StackOverflow, иметь root пост или вопрос и комментарии с ответом для пользователь не так много отвечает за комментарий и отвечает за ответы.
Как обычно, (ошибки):
SystemCheckError: System check identified some issues:
ERRORS:
calculator_app.Comment.comment: (fields.E303) Reverse query name for 'Comment.comment' clashes with field name 'Comment.comment'.
HINT: Rename field 'Comment.comment', or add/change a related_name argument to the definition for field 'Comment.comment'.
Решено добавлением related_name='replies'
к атрибуту комментария в Модель "Комментарий",
class Comment(models.Model):
# if I am a reply, so "comment" my parent comment, so each comment will have replies_set
comment = models.ForeignKey('self', on_delete=models.CASCADE, default = '', related_name='replies')
Еще одна ошибка, которая еще не устранена ::: вывод cmd
python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, calculator_app, contenttypes, home_page_app, sessions
Running migrations:
Applying calculator_app.0003_auto_20200402_1800...Traceback (most recent call last):
File "C:\Users\ms205\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\ms205\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\sqlite3\base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: near ")": syntax error
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\ms205\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\ms205\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\ms205\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\ms205\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py", line 364, in execute
output = self.handle(*args, **options)
File "C:\Users\ms205\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\ms205\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\commands\migrate.py", line 234, in handle
fake_initial=fake_initial,
File "C:\Users\ms205\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:\Users\ms205\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:\Users\ms205\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\ms205\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Users\ms205\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\operations\fields.py", line 178, in database_forwards
schema_editor.remove_field(from_model, from_model._meta.get_field(self.name))
File "C:\Users\ms205\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\sqlite3\schema.py", line 345, in remove_field
self._remake_table(model, delete_field=field)
File "C:\Users\ms205\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\sqlite3\schema.py", line 286, in _remake_table
self.quote_name(model._meta.db_table),
File "C:\Users\ms205\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\base\schema.py", line 137, in execute
cursor.execute(sql, params)
File "C:\Users\ms205\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 99, in execute
return super().execute(sql, params)
File "C:\Users\ms205\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\ms205\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\ms205\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\ms205\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\ms205\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\ms205\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\sqlite3\base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: near ")": syntax error
У кого-нибудь есть предложения по решению этой проблемы?