【发布时间】:2016-03-19 20:37:12
【问题描述】:
我正在构建一个 Django 应用程序,它将被多个外部应用程序联系。 Django 应用程序应该提供 UI 并使用从外部应用程序接收到的数据填充数据库。
第一个想法是使用django_rest_framework,但这似乎创建了一个紧密耦合的系统,因为每个外部应用程序都必须通过 REST 调用联系 Django 应用程序。
我的另一个想法最好用一张图片来描述:http://imgur.com/vakZvQs 几个发布者会在 RabbitMQ 上创建消息,我的 Django 会使用这些消息并在数据库中创建适当的模型。
这样的事情可能吗?我已经为发布者和消费者使用了来自pika 库的异步示例,并且消息按预期流动。将 Django 混入其中会产生如下错误:
RuntimeError: Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
代码摘录:
# pika consumer
def on_message(self, unused_channel, basic_deliver, properties, body):
# invoking view function
from myapp.views import create_one_foo
create_one_foo()
self.acknowledge_message(basic_deliver.delivery_tag)
# views.py
from .models import Foo
def create_one_foo():
foo = Foo()
foo.bar = "bar"
foo.save()
【问题讨论】:
-
您的问题似乎与 RabbitMQ 无关。我不了解 Django,但您应该提供有关您的 Django 应用程序的更多详细信息,以便有知识的人可以回答。