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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:43:10+00:00 2026-06-09T14:43:10+00:00

Hi Stackoverflow people, What is the best way to refer from one model to

  • 0

Hi Stackoverflow people,

What is the best way to refer from one model to a choice of ForeignKeys?

I am working on a rental app, which includes a model for the GenericVehicle, Bikes, and Cars.

class GenericVehicle(models.Model):
    licence = models.CharField(max_length=128)
    ...
    class Meta:
        abstract = True

class Bike(GenericVehicle):
    engine_type = models.CharField(max_length=128)
    ...

class Car(GenericVehicle):
    number_of_doors = models.SmallIntegerField()
    ...

Now I have a model for the rental registration, where I would like to register the rented vehicle. I am unsure about the best practise here. So far, I had two foreignkeys and made sure that at least one is filled. But this solution seems very inefficient and does not scale well for multiple vehicle types.

What is the best way to improve the class structure/definition?

class Rental(models.Model):
    rental_bike = models.ForeignKey(Bike)
    rental_car = models.ForeignKey(Car)
    rental_date = ...

Thank you for your advice. I am trying to find an efficient solution for some time already.

  • 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-09T14:43:12+00:00Added an answer on June 9, 2026 at 2:43 pm

    Django offers you a GenericForeignKey. A GenericForeignKey will need to fields on you model, one for saving the referenced model’s ContentType and a second one to save the object’s id:

    from django.db import models
    from django.contrib.contenttypes.models import ContentType
    from django.contrib.contenttypes import generic
    
    class Rental(models.Model):
        content_type = models.ForeignKey(ContentType)
        object_id = models.PositiveIntegerField()
    
        rental_vehicle = generic.GenericForeignKey('content_type', 'object_id')
    

    But keep in mind that this not a foreign key on database level, just Django kind of emulating some of the typical behaviour of a foreign key.

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

Sidebar

Related Questions

Hi Stackoverflow people, I am working on an organisation registration, in which orgs can
Hello stackoverflow people! I've got a problem which I haven't been able to solve
Hi people stackoverflow. I'm working with MVVM, I have ViewModel call UserViewModel with a
StackOverflow people, Need Java Advice and Code which reads a file(File.txt) and prints to
Hi Stackoverflow people, I am working with class based views and for a test
Whats the best way to implement rollover 'buttons' like Stackoverflow has for 'Questions', 'Tags',
Hi Stackoverflow people, I am implementing the MarkerCluster function for google map markers. I
Hi Stackoverflow people, I would like to change the m2m widget in the admin
Hello again Stackoverflow people! Assume I have these words: smartphones, smartphone I want to
Hello people of StackOverflow! I have used the searrch option, I've found some related

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.