我们如何在没有列表理解的情况下在 m2m 字段中的 django 管理面板中显示用户
how we can show users in admin panel in django in m2m field without list comprehension
class Page(models.Model):
user = models.ManyToManyField(User)
post = models.TextField()
post_date = models.DateField()
def get_user(self):
return ", ".join([str(p) for p in self.user.all()])
我添加此功能以获取用户并添加到列表显示中我的问题是我们使用列表理解如何在不使用列表理解的情况下进行操作
def get_user(self):return ", ".join([str(p) for p in self.user.all()])
有办法吗?
i want to know how we can do this without list comprehension. i mean how we can do with for loop
好的,这样:
def get_user(self):
users = list(self.user.all())
if len(users) == 0:
return ""
users_str = str(users[0])
for user in users[1:]:
users_str += ", " + str(user)
return users_str
不太确定为什么需要它,但有可能:)
class Page(models.Model):
user = models.ManyToManyField(User)
post = models.TextField()
post_date = models.DateField()
def get_user(self):
return ", ".join([str(p) for p in self.user.all()])
我添加此功能以获取用户并添加到列表显示中我的问题是我们使用列表理解如何在不使用列表理解的情况下进行操作
def get_user(self):return ", ".join([str(p) for p in self.user.all()])
有办法吗?
i want to know how we can do this without list comprehension. i mean how we can do with for loop
好的,这样:
def get_user(self):
users = list(self.user.all())
if len(users) == 0:
return ""
users_str = str(users[0])
for user in users[1:]:
users_str += ", " + str(user)
return users_str
不太确定为什么需要它,但有可能:)