我在Django模板中有这样的查询:
shared_username = [User.objects.filter(id__in= Share.objects.filter(users_id = log_id, files__file_name=k).values_list('shared_user_id', flat=True)).values_list('username') for k in file1]
它输出如下:
[[(u'fgdg',), (u'fsddfa',)], [(u'fgdg',)]]
第一个列表[(u'fgdg',), (u'fsddfa',)]
给出第一个列表的shared_username,依此类推。我想在文件中显示文件的用户名。
File Name Type Size Shared On Shared With
与我共享我希望显示每个文件的用户名。
我可以循环列表两次或三次获取所有用户名但是像这样我随机获得所有文件的所有用户列表。输出应该是:
File Name Type Size Shared On Shared With
ok.txt 2[link]
hello.txt 1[link]
ok1.txt 3[link]
我能够获得shared_with的计数。当用户点击链接时,我想显示与其共享文件的用户的用户名。
我怎样才能做到这一点?感谢
#models:
class User(AbstractBaseUser):
#id = models.IntegerField(primary_key=True)
#identifier = models.CharField(max_length=40, unique=True, db_index=True)
username = models.CharField(max_length=90, unique=True, db_index=True)
create_time = models.DateTimeField(null=True, blank=True)
update_time = models.DateTimeField(null=True, blank=True)
email = models.CharField(max_length=225)
#password = models.CharField(max_length=120)
external = models.IntegerField(null=True, blank=True)
deleted = models.IntegerField(null=True, blank=True)
purged = models.IntegerField(null=True, blank=True)
form_values_id = models.IntegerField(null=True, blank=True)
disk_usage = models.DecimalField(null=True, max_digits=16, decimal_places=0, blank=True)
objects = UserManager()
USERNAME_FIELD = 'email'
class Meta:
db_table = u'galaxy_user'
class File(models.Model):
users = models.ForeignKey(User)
file_name = models.CharField(max_length=100)
type = models.CharField(max_length=10)
source = models.CharField(max_length=100)
start_date = models.TextField()
time_overview = models.CharField(max_length=55)
end_date = models.TextField()
duration = models.TextField()
size_overview = models.IntegerField()
size = models.TextField()
flag = models.TextField()
flag_r = models.TextField()
class Share(models.Model):
users = models.ForeignKey(User)
files = models.ForeignKey(File)
shared_user_id = models.IntegerField()
shared_date = models.TextField()
log_id = request.user.id
shared_file = File.objects.filter(id__in= Share.objects.filter(users_id = log_id).values_list('files', flat=True)).annotate(count=Count('share__shared_user_id'))
file1 = [i.file_name for i in shared_file]
shared_username = [User.objects.filter(id__in= Share.objects.filter(users_id = log_id, files__file_name=k).values_list('shared_user_id', flat=True)).values_list('username') for k in file1]