我有一个基本的聊天模型:
class Thread(models.Model):
created_at = models.DateTimeField(default=timezone.now)
members = models.ManyToManyField(User, through='Membership', through_fields=('thread', 'user'))
class Membership(models.Model):
thread = models.ForeignKey(Thread, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
date_joined = models.DateTimeField(default=timezone.now)
class Message(models.Model):
thread = models.ForeignKey(Thread, on_delete=models.CASCADE)
sender = models.ForeignKey(User, on_delete=models.CASCADE)
body = models.TextField()
timestamp = models.DateTimeField(default=timezone.now)
我试图通过其foreignkey字段的属性过滤对象,对象有多对多的字段数据,然后由foreignkey的字段排序。
意思是,我只想获得那些消息时间戳大于用户加入线程的会员日期的线程,以及最后发送消息的订单。
示例:
Thread.objects.filter(members=user, message__timestamp__gte= < thread's user's membership date joined >).distinct()
我可以创建一个for循环并遍历每个线程,然后返回一个新列表,但是有什么方法可以在过滤自己时做到这一点,或者有更好的方法吗?
答案 0 :(得分:0)
这样的东西?
public static void main(String[] args) throws IOException {
String fileName = "createF dir\\text.txt";
String textToFile="apples, oranges";
String splitter[] = fileName.split(" ");
String actualPath = splitter[1];
File file = new File(actualPath);
if (file.getParentFile().mkdir()) {
file.createNewFile();
new FileOutputStream(actualPath).write(textToFile.getBytes());
} else {
throw new IOException("Failed " + file.getParent());
}
}