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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:39:02+00:00 2026-05-21T03:39:02+00:00

I have a broker model and a city model with the Broker model defined

  • 0

I have a broker model and a city model with the Broker model defined like this:

class Broker(models.Model):
    user = models.ForeignKey(Auth)
    areas_of_operation = models.ManyToManyField(City)
    phone = models.CharField(max_length=20)
    company_name =  models.CharField(max_length=50)
    address1 = models.CharField(max_length=100)
    address2 = models.CharField(max_length=100)
    city = models.ForeignKey(City)
    state = models.ForeignKey(State, unique=True)
    zip = models.IntegerField(max_length=5)

Of course this will yield an error but I was wondering if there was any way I can facilitate a relationship that indicates that a Broker can work in many cities but lives in only one city. Or is this a high level design issue in which I must create more tables to show that relationship?

  • 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-21T03:39:02+00:00Added an answer on May 21, 2026 at 3:39 am

    When you create a relationship from one model to the other, Django automatically adds a reverse relationship in the other direction. For example, if you got rid of the areas_of_operation field temporarily, it would work and you could use the code city.broker_set() to get all the brokers who live in a given city.

    However, when you create multiple links from one model to another, Django tries to create multiple reverse relationships under the same attribute name. This is borne out by the error message when you run manage.py validate on your models:

    Error: One or more models did not validate:
    myapp.broker: Accessor for field 'city' clashes with related m2m field 'City.broker_set'. Add a related_name argument to the definition for 'city'.
    myapp.broker: Accessor for m2m field 'areas_of_operation' clashes with related field 'City.broker_set'. Add a related_name argument to the definition for 'areas_of_operation'.
    

    In other words, the problem is not in having multiple links from Broker to City, but rather the automatic reverse relationship having the same default name for both links. To get around this, use the related_name argument to set the name to use for the reverse relationship. The following code worked for me:

    from django.contrib.auth.models import User
    from django.db import models
    
    class State(models.Model):
        name = models.CharField(max_length=100)
    
    class City(models.Model):
        name = models.CharField(max_length=100)
    
    class Broker(models.Model):
        user = models.ForeignKey(User)
        areas_of_operation = models.ManyToManyField(City, related_name='operators')
        phone = models.CharField(max_length=20)
        company_name =  models.CharField(max_length=50)
        address1 = models.CharField(max_length=100)
        address2 = models.CharField(max_length=100)
        city = models.ForeignKey(City, related_name='citizens')
        state = models.ForeignKey(State, unique=True)
        zip = models.IntegerField(max_length=5)
    

    Now, if you have a City object, you can call city.operators() to get a list of who operates in that city, and city.citizens() for a list of who lives there.

    See the Django model field documentation for further information on relationship fields.

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

Sidebar

Related Questions

I have this model: class Auth(models.Model): TYPES = ( ('agent', 'Agent'), ('broker', 'Broker'), )
I have a model like this class Canvas include Mongoid::Document field :name referenced_in :hero
I have an index page for listing products. From this page I would like
I have some VBA code that looks like this and the aim is to
When I generate a default scaffold, the display tags on show.html.erb have <%=h @broker.name
Has anyone come across this issue? Seems MS have broken it with their own
i have an entity-model file (edmx) file which contains few tables and few stored
this is a really stupid question but I have a deadline and I'm very
For example I have this gem called Authlogic-openid, that plugin is outdated, no longer
So heres the scenario: Currently we have a development site with 3 models. We

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.