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

  • Home
  • SEARCH
  • 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 4052292
In Process

The Archive Base Latest Questions

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

I started learning Django recently, and can’t find answer for a simple question. I

  • 0

I started learning Django recently, and can’t find answer for a simple question.
I have 2 tables: Client and Addres.

------------------
CLIENT |
------------------
ID |
NAME |
ADDRES_REF |
------------------

------------------
ADDRES |
------------------
ID |
NAME |
CITY |
COLLECTION |
------------------

The relation between them is: client.addres_ref=addres.collection.
In order to select all addresses of client with ID equal 123 I have to create such query:

select addres.name, addres.city from addres, client where client.addres_ref=addres.collection and client.id=123;

Certainly its posible to create relation many-to-many, but I dont wont create additional table for it and change the structure of tables.

class Addres(models.Model):       
    address = models.CharField(max_length=150)
    city    = models.ForeignKey(City)




class Client(models.Model):
    addres          =models.ManyToMany(Addres)        
    email           =models.EmailField(blank=True)
    name            =models.CharField(max_length=50)

It is posible to add ForeignKey(Client) in Addres model, but I need reference to Addres from another models too, like User, Employer …
Help me please to create models with relations from above-stated tables.

  • 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-20T14:18:08+00:00Added an answer on May 20, 2026 at 2:18 pm

    I think what you need to do is use the content types framework to create generic foreign key in the Addres model.

    from django.contrib.contenttypes.models import ContentType
    from django.contrib.contenttypes import generic
    
    class Addres(models.Model):
        content_type = models.ForeignKey(ContentType, related_name='addresses')
        object_id = models.IntegerField()
        content_object = generic.GenericForeignKey( )
    
        address = models.CharField()
        city = models.ForeignKey(City)
    
    class Client(models.Model):
        addresses = generic.GenericRelation( Addres )
        email = models.EmailField()
        name = models.CharField()
    

    then client objects will return addresses by using client.addresses or you can query the Addres model like this:

    client = Client.objects.get(pk=123)
    addresses = Addres.objects.filter(content_object=client)
    

    and the Addres model can be linked to other models as well, e.g.

    class User(models.Model):
         addresses = generic.GenericRelation( Addres )
         name = models.CharField()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've recently started learning/using django; I'm trying to figure out a way to have
I recently started learning Python and I was rather surprised to find a 1000
I have recently started learning F#, and this is the first time I've ever
I have recently started learning Perl and one of my latest assignments involves searching
I started learning Django recently and am having a strange problem with the tutorial.
I have just started learning C#. Can anyone explain the technical differences between a
I've just started learning Lisp and I can't figure out how to compile and
I recently started learning Emacs . I went through the tutorial, read some introductory
I have just started learning Erlang and am trying out some Project Euler problems
So, I started learning to code in Python and later Django . The first

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.