Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to print list according to index in Django template?
    primarykey
    data
    text
    <p>I have a query like this in Django template:</p> <pre><code> 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] </code></pre> <p>It gives the output like this:</p> <pre><code>[[(u'fgdg',), (u'fsddfa',)], [(u'fgdg',)]] </code></pre> <p>The first list <code>[(u'fgdg',), (u'fsddfa',)]</code> gives the shared_username of the first list and so on. I want to show the username for a file to the file.</p> <pre><code>File Name Type Size Shared On Shared With </code></pre> <p>In shared with I want to show the username for each file. </p> <p>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:</p> <pre><code> File Name Type Size Shared On Shared With ok.txt 2[link] hello.txt 1[link] ok1.txt 3[link] </code></pre> <p>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. </p> <p>How can I achieve this? Thanks</p> <p>#models:</p> <pre><code>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() </code></pre> <h1>views.py:</h1> <pre><code> 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] </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload