按插入的订单值订购多对多字段
Order many-to-many field by the order values were inserted
# models.py
from django.contrib.auth.models import User
# Create your models here.
class Profile(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
avatar = models.ImageField(upload_to=avatar_photo_upload, blank=True)
banner = models.ImageField(upload_to=banner_photo_upload, blank=True)
bio = models.TextField(max_length=150, blank=True)
date_joined = models.DateField(auto_now_add=True)
time_joined = models.TimeField(auto_now_add=True)
def __str__(self) -> str:
return self.user.username
class Friend(models.Model):
# The friends, sender first, reciever second
friends = models.ManyToManyField(Profile)
# accepted or rejected
accepted = models.BooleanField(default=False)
rejected = models.BooleanField(default=False)
date_added = models.DateField(auto_now=True)
time_added = models.TimeField(auto_now=True)
def __str__(self):
if self.accepted == False and self.rejected == False:
return f'{" sent a request to ".join([i.user.username for i in self.friends.all()])}'
else:
if self.accepted == True and self.rejected == True:
return f'{" was a friend of ".join([i.user.username for i in self.friends.all()])}'
elif self.accepted == True and self.rejected == False:
return f'{" is a friend of ".join([i.user.username for i in self.friends.all()])}'
else:
return f'{" was rejected as a friend by ".join([i.user.username for i in self.friends.all()])}'
friends
字段是 many-to-many field
,
通过示例了解我的问题:
例如 -> 创建配置文件对象 a
,然后创建配置文件对象 b
。
现在,我在 friend
字段中添加配置文件对象 b
,然后添加对象 a
,但是 friend
字段将它们排序在 a
中,然后是 b
。 ..
如何按添加时间排序。
谢谢。
编辑:
预期输出:与插入值的顺序相同。
Django Many To Many Insert Ordering
Django 不保证保持多对多关系的顺序。如果你需要这个,你应该使用带有显式 'order' 字段的 table。
# models.py
from django.contrib.auth.models import User
# Create your models here.
class Profile(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
avatar = models.ImageField(upload_to=avatar_photo_upload, blank=True)
banner = models.ImageField(upload_to=banner_photo_upload, blank=True)
bio = models.TextField(max_length=150, blank=True)
date_joined = models.DateField(auto_now_add=True)
time_joined = models.TimeField(auto_now_add=True)
def __str__(self) -> str:
return self.user.username
class Friend(models.Model):
# The friends, sender first, reciever second
friends = models.ManyToManyField(Profile)
# accepted or rejected
accepted = models.BooleanField(default=False)
rejected = models.BooleanField(default=False)
date_added = models.DateField(auto_now=True)
time_added = models.TimeField(auto_now=True)
def __str__(self):
if self.accepted == False and self.rejected == False:
return f'{" sent a request to ".join([i.user.username for i in self.friends.all()])}'
else:
if self.accepted == True and self.rejected == True:
return f'{" was a friend of ".join([i.user.username for i in self.friends.all()])}'
elif self.accepted == True and self.rejected == False:
return f'{" is a friend of ".join([i.user.username for i in self.friends.all()])}'
else:
return f'{" was rejected as a friend by ".join([i.user.username for i in self.friends.all()])}'
friends
字段是 many-to-many field
,
通过示例了解我的问题:
例如 -> 创建配置文件对象 a
,然后创建配置文件对象 b
。
现在,我在 friend
字段中添加配置文件对象 b
,然后添加对象 a
,但是 friend
字段将它们排序在 a
中,然后是 b
。 ..
如何按添加时间排序。
谢谢。
编辑:
预期输出:与插入值的顺序相同。
Django Many To Many Insert Ordering
Django 不保证保持多对多关系的顺序。如果你需要这个,你应该使用带有显式 'order' 字段的 table。