Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8857427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:35:12+00:00 2026-06-14T14:35:12+00:00

I have multiple objects saved in my database but I would only like to

  • 0

I have multiple objects saved in my database but I would only like to show items in my queryset that are unique and if they actually have an item saved.

models.py

class Everything(models.Model):
    profile = models.ForeignKey(User)
    playlist = models.CharField('Playlist', max_length = 2000, null=True, blank=True)
    platform = models.CharField('Platform', max_length = 2000, null=True, blank=True)
    video = models.CharField('VideoID', max_length = 2000, null=True, blank=True)
    def __unicode__(self):
        return u'%s %s %s %s' % (self.profile, self.playlist, self.platform, self.video)

views.py

playlist2 = Everything.objects.filter(profile=request.user)

template

<select name ="playlist2">
{% for item in playlist2 %}
  <option value="{{item.playlist}}">{{item.playlist}}</option>
{% endfor %}
</select>

There doesn’t have to be a playlist because null=True and blank=True. Some of the items in playlist might also be duplicated. How do I show only the distinct items that have values in them?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:35:14+00:00Added an answer on June 14, 2026 at 2:35 pm

    You can use exclude and distinct queryset functions (docs).

    However in your case it is not that simple since your CharField definitions allow both NULL (null=True) and empty string (blank=True) values. So that means you have to test for two conditions and for that you have to use Q (docs) objects:

     everything = Everything.objects.exclude(Q(playlist=None) | Q(playlist='')).distinct()
    

    That is exactly why Django docs do not recommend to use null=True for string model fields (docs). Here is excerpt:

    Avoid using null on string-based fields such as CharField and
    TextField unless you have an excellent reason. If a string-based field
    has null=True, that means it has two possible values for “no data”:
    NULL, and the empty string. In most cases, it’s redundant to have two
    possible values for “no data;” Django convention is to use the empty
    string, not NULL.

    If you follow this conversion, then your filter lookup becomes much simpler:

     everything = Everything.objects.exclude(playlist=None).distinct()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that I want to contain multiple objects of something I
i have created multiple sequential sql query objects for an ms access database. e.g
I have multiple objects moving about in a 3D space and am looking for
I have multiple NSURLConnections running providing data back to the delegate objects. Is it
I'd like to have multiple versions of an object with different access modifiers on
I have a NSString object having multiple conditions specified in it like below: NSString
I have been attempting to have a object that I can use across multiple
In my SQLite database only one row effect but I want effect 4 rows
I have a program in which the user adds multiple objects to a scene.
I have some calculated values in the core data database that I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.