Skip to content

Chat Models Documentation

Overview

This document explains the purpose and structure of the Chat and Message models in your project. These models enable one-on-one communication between vendors and users, supporting various message types such as text, images, audio, and video.


Chat Model

The Chat model represents a conversation between a vendor and a user.

Fields

Field Name Type Description
id Integer (PK) Primary key of the chat record.
vendor ForeignKey A reference to the Vendor model.
user ForeignKey A reference to the User model.
created_at DateTime The timestamp when the chat was created.
updated_at DateTime The timestamp when the chat was last updated.

Constraints

  • Unique Together: Ensures that each vendor and user combination can have only one active chat.

Database Table

The Chat model maps to the tbl_chat table in the database.


Message Model

The Message model represents individual messages exchanged in a Chat.

Fields

Field Name Type Description
id UUID (PK) Primary key of the message record.
chat ForeignKey A reference to the Chat model.
sender ForeignKey A reference to the User who sent the message.
receiver ForeignKey A reference to the User who receives the message.
message_type CharField Specifies the type of message (text, image, audio, video).
content TextField The textual content of the message (optional, depending on type).
image ImageField An uploaded image file (optional, depending on type).
audio FileField An uploaded audio file (optional, depending on type).
video FileField An uploaded video file (optional, depending on type).
is_read BooleanField Indicates whether the message has been read by the receiver.
created_at DateTime The timestamp when the message was created.
updated_at DateTime The timestamp when the message was last updated.

Validation Rules

  • Content Exclusivity: Only one of content, image, audio, or video can be set per message.
  • File Validation: Each file type (image, audio, video) is validated using custom validators to ensure proper formats.

Database Table

The Message model maps to the tbl_chat_messages table in the database.


Relationships

  1. Chat to Message: One Chat can contain multiple Message records.
  2. User to Message: Each Message has a sender and a receiver, both of which are User instances.
  3. Vendor to Chat: Each Chat is associated with a Vendor.

Summary

These models collectively allow for structured and validated communication between vendors and users, supporting various types of content while maintaining database integrity and performance.