0

I have a Django 4 project and using KafkaConsumer from kafka-python. I want to update django models after receiving a Kafka message. The goal here is to have some Kafka worker running and consuming message, it is also should able to have access to the models in the existing django ASGI app. Is it possible or should this worker be a separate django project?

Tim
  • 1
  • 1

1 Answers1

1

Yes, this is possible. You can simply write a python script and import a model like this

from PROJECT_PATH import settings as PROJECT
from django.core.management import settings

# Import your django model
from SOME_APP.models import SOME_MODEL

# import Kafka consumer 
from kafka import KafkaConsumer, TopicPartition

# Create kafka consumer
consumer = KafkaConsumer("topicName", bootstrap_servers='<BOOTSTRAP_SERVER>')
for msg in consumer:
    # play with message and use Django model here 
iammehrabalam
  • 1,285
  • 3
  • 14
  • 25