chats = []
messages = Message.objects.filter(Q(user_from=user)|Q(user_to=user)).order_by("created")
chatters = set(messages.values_list('user_from', flat=True)).union(messages.values_list('user_to', flat=True))
for chatter in list(chain(chatters)):
filtered = messages.filter(Q(user_from__id=chatter,user_to=user)|Q(user_from=user,user_to__id=chatter)).order_by("-created")
if len(filtered)>0:
chats.append(filtered[0])
User1 -> User2
User2 -> User1
User3 -> User2
User3 User3Message
User1 User2Message
class Message(models.Model):
user_from = models.ForeignKey(User, related_name="user_from")
user_to = models.ForeignKey(User, related_name="user_to")
created = models.DateTimeField(default=datetime.now())
message = models.TextField(blank=False)
delivered = models.DateTimeField(null=True, blank=True)
viewed = models.DateTimeField(null=True, blank=True)
deleted_out = models.DateTimeField(null=True, blank=True)
deleted_in = models.DateTimeField(null=True, blank=True)
size = models.FloatField(default=1.0)
def __str__(self):
return str(self.user_from) + "-" + str(self.user_to)
def createSmthng(request):
if request.POST:
if "video" in request.FILES:
video = request.FILES["video"]
else:
video = None
...
Post.objects.create(...,file=video)
#return redirect("feed")
<form enctype="multipart/form-data" action="{% url 'someUrl' %}" method="POST">
...
<input name="video" type="file">
...
</form>