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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:19:48+00:00 2026-06-15T15:19:48+00:00

I have to implement two models in a django project –> Country and Continent.

  • 0

I have to implement two models in a django project –> Country and Continent.
It is possible to import all information about countries and continents from an XML file that populates the database with these two tables.

I’ve implemented them in this way

from django.db import models

    class Continent(models.Model):
        name = models.CharField(max_length=255, unique=True)
        code = models.CharField(max_length=255, unique=True)
        countries = ?

        class Meta:
            ordering = ['name']

    class Country(models.Model):
        name = models.CharField(max_length=255)
        capital = models.CharField(max_length=255)
        code = models.CharField(max_length=255, unique=True)
        population = models.IntegerField(default=0)
        area = models.IntegerField(default=0)
        continent = models.ForeignKey(Continent)

        class Meta:
            ordering = ['name']

You can see a ? mark in Continent.countries because I don’t understand how to implement it. The problem is: “Countries of a continent should be accessible through attribute countries of class Continent”

This means that it is possible to do this:

from MyApplication.model import Country, Continent
europe = Continent.object.get(code=u'eu')
finland = europe.countries.get(code=u'fi')

I’ve tried models.ForeignKey, but it doesn’t work.
It says that “Nonetype object doesn’t have any get method”
Can anyone help me please?

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-06-15T15:19:49+00:00Added an answer on June 15, 2026 at 3:19 pm

    Django does not support one to many directly, instead you can use the reverse of one-to-one relation to get a one-to-many relation.

    So, go ahead with the other approach of using many to one by adding foreign key inside the country relating to continent. Now, you have a one-to-one from country to continent. Next, do a reverse query from Continent‘s object to get the desired relation. Take a look:

    class Continent(models.Model):
        ...
    
    class Country(models.Model):
        ...
        continent = models.ForeignKey(Continent, related_name="countries")
        ...
    
    europe = Continent.object.get(code=u'eu')
    finland = europe.countries.get(code=u'fi')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models (and tables) in my Rails project. Each model has an
I have two models milestone and project, the code as below: class Project(models.Model): ID
I have implement a scenario which involves two way communication between child and parent
If I have two classes that both implement an interface, but also inherit, do
I have two range sliders in my page, and I would like to implement
I have a class that instantiates two classes which implement interfaces. I want one
I have been trying for the past two days now to implement Admob ads
Here I am trying to implement the jQuery I have two main files one
I have a Django application in which I need to implement a simple trending/ranking
I have two models. order and line_item. class Order < ActiveRecord::Base has_many :line_items has_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.