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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:13:22+00:00 2026-05-13T12:13:22+00:00

I have a Django project, that has a Address model. This is used in

  • 0

I have a Django project, that has a “Address” model. This is used in several places – by a “User Profile” model, by a “Hospital” model, by an “Insitution” model etc.

I’m using Django’s generic relations to allow each of these objects to create a foreign-key to Address.

However, this seems to cause some weirdness in the Django Admin (or perhaps I’m not understanding correctly how it’s meant to be used). In the Django Admin, if I try to create an address, I’m seeing fields for “Content type” and “Object id”. The model won’t validate/save if these aren’t filled. Not sure what to put in these.

The thing is, I wanted to be able to create standalone Address objects. Then, when I create a User Profile, or a Hospital, I could link these to the Address objects, including the possibility of multiple ones linking to the same Address object.

How should I use the Django admin with generic relations?

Also, I’m also intending to use django-reversion for version control of the models, not sure if this will cause any issues with generic relations and the admin?

Cheers,
Victor

Edit: I should just add, here’s an earlier question I posted Addresses and Inlines:

Django – Designing Model Relationships – Admin interface and Inline

Based on the answers given there, that’s why the Address model is the one with a foreign key. And since a normal FK field can only point to one type of object, that’s why we’re using generic relations.

Each User/Department/Hospital etc. may (and in most cases will) have multiple addresses.

The same address can be used by multiple entities, but this is rarer, and duplication is fine here, I’m guessing, right?

So it’d be a one-to-many from User/Department/Hospital to Addresses.

In that original question, they also suggested using abstract classes, and a different Address model for each entity that needed an Address. I’m still not sure if that’s the better approach, or if there’s a way to make GenericRelations work with what I’m trying to do here.

  • 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-13T12:13:22+00:00Added an answer on May 13, 2026 at 12:13 pm

    Using generic relations in django is exactly that. A ForeignKey to ContentType (content_type) and an IntegerField to denote the instance id (object_id). These are useful if you don’t know which type of content the ForeignKey is pointing to. Since you know you’re targeting the Address model you want to use regular ForeignKey(Address) instead of generic relations.

    In response to your comment

    Actually its much easier to use ForeignKey since you don’t need to go through ContentType.

    class Address(models.Model):
      street=models.CharField(max_length=100)
      city=models.CharField(max_length=100)
    
    
    class Hospital(models.Model):
      name=models.CharField(max_length=100)
      created=models.DateTimeField()
      address=models.ForeignKey(Address, related_name="hospitals")
    
    class Institution(models.Model):
      name=models.CharField(max_length=100)
      address=models.ForeignKey(Address, related_name="institutions")
    
    
    >>> instance=Institution.objects.get(id=1)
    >>> instance.address.city
    >>> address=Address.objects.get(id=1)
    >>> address.institutions.all() 
    >>> address.hospitals.all()
    

    Are your models going to share addresses? i.e. Can a Hospital and an Institution and perhaps a UserProfile all point to the same address instance? Or is it more likely that each will have it’s own address? I am trying to understand why you’ve created a separate class Address. If it’s to avoid retyping the same fields into each class you could use an abstract model class and subclass it. Or you might be needing a OneToOneField which is a two way pointer between the two instances.

    • http://docs.djangoproject.com/en/dev/ref/models/fields/#onetoonefield
    • Abstract Model Classes
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've made a simple user profile editor for a django project. And it has
I have worked a bit with Django and I quite like its project/applications model
I have a django project that runs on a Linux server, and I've been
I have a Django project that uses Celery for running asynchronous tasks. I'm doing
I'm looking for an excuse to learn Django for a new project that has
I have a medium sized Django project, (running on AppEngine if it makes any
I have a Django model with a large number of fields and 20000+ table
I have written a Django app that makes use of Python threading to create
I have the following Django and Flex code: Django class Author(models.Model): name = models.CharField(max_length=30)
I stumbled over this passage in the Django tutorial : Django models have a

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.