How to print list according to index in Django template?

帅比萌擦擦* 提交于 2019-12-25 14:47:12

问题


I have a query like this in Django template:

 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]

It gives the output like this:

[[(u'fgdg',), (u'fsddfa',)], [(u'fgdg',)]]

The first list [(u'fgdg',), (u'fsddfa',)] gives the shared_username of the first list and so on. I want to show the username for a file to the file.

File Name   Type    Size    Shared On   Shared With

In shared with I want to show the username for each file.

I can loop the list twice or thrice and get all the username but like this I get all the user list for all files randomly. The output should be like:

  File Name Type    Size    Shared On   Shared With
   ok.txt                                         2[link]
   hello.txt                                      1[link]
   ok1.txt                                         3[link]

I am able to get the count of shared_with. When the user clicks on the link I want to show the usernames of the user with whom the file is shared.

How can I achieve this? Thanks

#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()

views.py:

 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]

来源:https://stackoverflow.com/questions/15474953/how-to-print-list-according-to-index-in-django-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!