How to properly implement MVC architecture in a PySide6/QML and Django application?

I am developing a desktop application with PySide6/QML for the frontend and Django (using Channels for real-time chat features) for the backend. I aim to adhere to the Model-View-Controller (MVC) architecture, but I'm uncertain about how to integrate MVC properly with the technologies I've chosen.

So far, I've created a login interface and a simple real-time chat application using WebSockets, all within QML and built-in JavaScript functions. Python has been used sparingly, mainly for tasks I couldn't accomplish in QML/JS (e.g., clipboard access and base64 encoding for image handling). I have a big working application,but I have only one python file, I guess it is bad habit

class Window(QObject):
    def __init__(self) -> None:
        super().__init__()

        
    @Slot(result=str)
    def getPaste(self):
        cb = QClipboard(self)
        return cb.text()
    
    @Slot(str,result=str)    
    def encodeImage(self,path):
        with open(path, 'rb') as image_file:
            base64_bytes = base64.b64encode(image_file.read())
        return base64_bytes.decode()



if __name__ == "__main__":
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()
    window = Window()
    engine.rootContext().setContextProperty('window',window)
    qml_file = Path(__file__).resolve().parent / "main.qml"
    engine.load(qml_file)
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec())

Here's a brief overview of my current setup:

Frontend (QML/JS):

WebSocket communication with the Django server using WebSocket { id: chatSocket }. Sending data with chatSocket.sendTextMessage(JSON.stringify()). Handling incoming messages with onTextMessageReceived: {} in QML. Reusable components and pages are separated into individual QML files. Resources like images and fonts are managed in separate files.

Backend (Django):

No integration with the frontend yet, except for the WebSocket chat.

Project Structure:

Currently, the project consists of one single Python file and multiple QML files. I am looking for guidance on how to structure my application to fit the MVC pattern with these technologies. Specifically, I need advice on:

How to define and interact with models in a PySide6/QML application. The best practices for setting up controllers in this context. How to ensure that the views (QML) are properly separated from the business logic. Any examples or references to projects using a similar tech stack would be greatly appreciated.

Back to Top