如何使用Django中的模型列表?

时间:2017-05-15 02:28:14

标签: django django-models

我想这样做:

Class conversation(models.Model):
      user1, user2 =; # this is from User table
      messages = List(Message) # where Message is also a model

Class Message(models.Model):
      sender, reciever ; # this is user ID from User Model
      ...... # many other attribute
      message = TextField()

这只是一个伪代码。有人,请向我解释如何做到这一点。 如果可能,请给我一个与此相关的合适教程链接。感谢。

1 个答案:

答案 0 :(得分:1)

const Rx = require("rx");

const observable = Rx.Observable.create(function(observer) {
  observer.next(1);
  observer.next(2);
  const intervalId = setInterval(() => {
    observer.next('nmb');
  }, 1000);

  return function unsubscribe() {
    clearInterval(intervalId);
  }
});

const subscription = observable.subscribe(x => console.log(x));

setTimeout(() => {
  subscription.unsubscribe();
}, 5000);

要查看会话实例的消息,您可以像这样

向后关注关系
Class Conversation(models.Model):
      user1, user2 =; # this is from User table
      ...... # many other attribute

Class Message(models.Model):
      sender, reciever ; # this is user ID from User Model
      ...... # many other attribute
      conversation = models.ForeignKey(Conversation, on_delete=models.CASCADE, related_name='messages')