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?
Asked
Active
Viewed 755 times
0
-
What do you mean by "update Django models"? You mean some database? – OneCricketeer Aug 05 '22 at 17:49
-
Please provide enough code so others can better understand or reproduce the problem. – Community Aug 05 '22 at 23:54
1 Answers
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
-
Ideally, you subscribe the consumer to all partitions, not assign it to partitions – OneCricketeer Aug 05 '22 at 17:48
-
@OneCricketeer you are right but here I just gave an example or you can say the approach, Anyway, i updated answer – iammehrabalam Aug 05 '22 at 17:58