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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:17:18+00:00 2026-05-22T14:17:18+00:00

I have three related models like class City(models.Model): name = models.CharField(max_length=200, blank=False) country =

  • 0

I have three related models like

class City(models.Model):
    name = models.CharField(max_length=200, blank=False)
    country = models.ForeignKey(Country,unique=False,null=False)

    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ['name']        

class County(models.Model):
    name = models.CharField(max_length=500, blank=False)
    city = models.ForeignKey(City,unique=False,null=False)

    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ['name']

class District(models.Model):
    name = models.CharField(max_length=500, blank=False)
    county = models.ForeignKey(County,unique=False,null=False)

    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ['name']  

What I’d like to do is getting all the Districts for a specified city.
I tried :

District.objects.all().filter(county = County.objects.all().filter(city=City.objects.filter(id=4)))

However, it gives error like OperationalError: (1242, 'Subquery returns more than 1 row')

Can you give me any idea how I can achive this query in django ?

Thanks

  • 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-22T14:17:19+00:00Added an answer on May 22, 2026 at 2:17 pm

    I’m not sure why you’re complicating things by doing them that way. You could get away with something along the lines of:

    For a given instance city of the model City, you can get all Districts in this way:

    District.objects.filter(county__city=city)

    You may want to go through this section on the Django documentation called Lookups that span relationships as it explains how you can achieve similar lookup queries.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models class Employer(models.Model): name = models.CharField(max_length=300, blank=False) id = models.IntegerField() status
I currently have these models: class Category(models.Model): name = models.CharField(max_length=200) parent = models.ForeignKey('self', blank=True,
class Book(models.Model): name = models.CharField(max_length=127, blank=False) class Author(models.Model): name = models.CharField(max_length=127, blank=False) books =
I have a simple category model: class Category(models.Model): name = models.CharField(max_length=200) slug = models.SlugField()
Suppose I have something like this in my models.py: class Hipster(models.Model): name = CharField(max_length=50)
I have a model basically like this: class Unit(models.Model): name = models.CharField(max_length=64) class UnitPrice(models.Model):
You have models: class Order(Model): date = DateField(editable = False, auto_now_add=True) status = CharField(max_length
If you have some models: class Teacher(models.Model): name = models.CharField(max_length=50) class Student(models.Model): age =
I have a model that is something like this: class ReturnAuthorization(models.Model): custom_id = models.CharField(max_length=40)
I have a model similar to this one: class MyModel(models.Model): name = models.CharField(max_length =

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.