Есть задача организовать диалоги между пользователями. Сообщения должны хранятся на сервере неделю. Какая лучше схема организации таблиц в БД?
гуглом нашел типа этого:
class Dialog(TimeStampedModel):
owner = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_("Dialog owner"), related_name="selfDialogs",
on_delete=models.CASCADE)
opponent = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_("Dialog opponent"), on_delete=models.CASCADE)
def __str__(self):
return _("Chat with ") + self.opponent.username
class Message(TimeStampedModel, SoftDeletableModel):
dialog = models.ForeignKey(Dialog, verbose_name=_("Dialog"), related_name="messages", on_delete=models.CASCADE)
sender = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_("Author"), related_name="messages",
on_delete=models.CASCADE)
text = models.TextField(verbose_name=_("Message text"))
read = models.BooleanField(verbose_name=_("Read"), default=False)
all_objects = models.Manager()
def get_formatted_create_datetime(self):
return dj_date(localtime(self.created), settings.DATETIME_FORMAT)
def __str__(self):
return self.sender.username + "(" + self.get_formatted_create_datetime() + ") - '" + self.text + "'"