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 6161233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:26:34+00:00 2026-05-23T21:26:34+00:00

This should be really simple, I could do it without thinking on traditional SQL

  • 0

This should be really simple, I could do it without thinking on traditional SQL but can’t get it with Django models!

I have a model with two foreign keys to create many to many relationship – I am not using many to many field in Django

class Story(models.Model):
   title = models.CharField(max_length=200)
   pub_date = models.DateTimeField('date published')

   def __unicode__(self):
      return self.title

class Category(models.Model):
   categoryText = models.CharField(max_length=50)
   parentCat = models.ForeignKey('self',null=True,blank=True)

   def __unicode__(self): 
      return self.categoryText

class StoryCat(models.Model):
    story = models.ForeignKey(Poll,null=True,blank=True)
    category = models.ForeignKey(Category,null=True,blank=True)  

    def __unicode__(self):
      return self.story

I would like to query for a category like ‘short’, and retrieve all the unique keys to all stories returned.

c=Category.objects.get(categoryText=’short’)

s=StoryCat.objects.get(category=c)

when I try this I get an error that multiple rows were returned. I actually want to just retrieve the id values but I don’t seem to be able to figure how to do this.

How can I get the id values that are returned?

  • 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-05-23T21:26:35+00:00Added an answer on May 23, 2026 at 9:26 pm

    “multiple rows were returned” is because you are using get rather then filter on either or both of the queries.

    If there is only one category matching short try:

    c = Category.objects.get(categoryText='short')
    s = StoryCat.objects.filter(category=c)
    

    If it is possible for there to be more then one category try something like the following:

    # get just ids via values_list
    cids = Category.objects.filter(categoryText='short').values_list('id', flat=True)
    
    s = StoryCat.objects.filter(category__id__in=cids)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be a really really simple thing, but for some reason it is
This question can be very simple but could not find the answer; I receive
This should be straight forward for a guru. I don't have any code really
This should be a simple question, but I haven't been able to find a
I'm having a slight problem that I can't figure out, but should be really
This should hopefully be a simple one. I would like to add an extension
This should be easy for many of you, but for me it's just another
This should be easy, but I'm not sure how to best go about it.
It seems like this should be straightforward but I'm boggling. I've got my listview
I think this should be easy, but it's evading me. I've got a many-to-many

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.