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()])
i add this function to get users and added in list-display my question is we use list comprehension how we can do without using list comprehension
def get_user(self):return ", ".join([str(p) for p in self.user.all()])
is there anyway to do it?
CodePudding user response:
i want to know how we can do this without list comprehension. i mean how we can do with for loop
Okay, this way:
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
Not pretty sure why you need it, but it's possible :)