django - Multipe one-to-many relation between two models - Stack Overflow
I was creating a route finder and I created a vehicle model with relations start_location and final_location. I want them to be foreign keys as I don't want to have multiple options when selecting start and final location and a foreign key defines a one-to-many relation which means one location can have multiple vehicles but one vehicle can't be related to multiple locations.
Will Django give me an error if I create multiple foreign key fields and connect to location?
Will it create a many-to-many relation?
class Destination(models.Model):
name = models.CharField(max_length=100)
state = models.ForeignKey(State, on_delete=models.CASCADE,related_name="destination")
country = models.ForeignKey(Country, on_delete=models.CASCADE,related_name="destination")
class vehicle(models.Model):
start_time = models.CharField(max_length=10)
reaching_time = models.CharField(max_length=10)
startdestination = models.ForeignKey(Destination, on_delete=models.CASCADE,related_name="startdestinations")
subdestination = models.ForeignKey(Destination, on_delete=models.CASCADE,related_name="subdestinations")
finaldestination = models.ForeignKey(Destination, on_delete=models.CASCADE,related_name="finaldestinations")
fare = models.IntegerField()
confirmed = models.BooleanField()
distance = models.IntegerField()
I was creating a route finder and I created a vehicle model with relations start_location and final_location. I want them to be foreign keys as I don't want to have multiple options when selecting start and final location and a foreign key defines a one-to-many relation which means one location can have multiple vehicles but one vehicle can't be related to multiple locations.
Will Django give me an error if I create multiple foreign key fields and connect to location?
Will it create a many-to-many relation?
class Destination(models.Model):
name = models.CharField(max_length=100)
state = models.ForeignKey(State, on_delete=models.CASCADE,related_name="destination")
country = models.ForeignKey(Country, on_delete=models.CASCADE,related_name="destination")
class vehicle(models.Model):
start_time = models.CharField(max_length=10)
reaching_time = models.CharField(max_length=10)
startdestination = models.ForeignKey(Destination, on_delete=models.CASCADE,related_name="startdestinations")
subdestination = models.ForeignKey(Destination, on_delete=models.CASCADE,related_name="subdestinations")
finaldestination = models.ForeignKey(Destination, on_delete=models.CASCADE,related_name="finaldestinations")
fare = models.IntegerField()
confirmed = models.BooleanField()
distance = models.IntegerField()
Share
Improve this question
edited 7 hours ago
philipxy
15.1k6 gold badges42 silver badges94 bronze badges
asked 15 hours ago
nikhil gusainnikhil gusain
11
New contributor
nikhil gusain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
- 2 Have you tried it? – Tangentially Perpendicular Commented 15 hours ago
1 Answer
Reset to default 0Will Django give me an error if I create multiple foreign key fields and connect to location?
No, given the related_name=…
[Django-doc] and the related_query_name=…
[Django-doc] are unique. Sicne the related_query_name=…
defaults to the related_name=…
if given, it is thus sufficient to provide a unique related_name=…
and not construct ForeignKey
s with a given equivalent related_query_name=…
.
Note: The
related_name=…
parameter [Django-doc] is the name of the relation in reverse, so from theDestination
model to theVehicle
model in this case. Therefore it (often) makes not much sense to name it the same as the forward relation. You thus might want to consider renaming therelation tostartdestination
vehicles_started
.
- Win11革命性新变化来了!31年的NTFS被取代:ReFS将成默认文件系统
- 外媒称谷歌正筹划自主开发安卓手机
- Spring Boot 3: Exclude REST Endpoints from Authorization - Stack Overflow
- Upgrading apache spark core from 3.3.2 to >=3.4.4 results in stackoverflowerror in logging - Stack Overflow
- r markdown - Rstudio custom traceback - Stack Overflow
- audio - How can i improve my sound module in Java? - Stack Overflow
- perl - How to embed Teraterm in a Visual Studio project - Stack Overflow
- node.js - Mongoose schema worked with the main db but not with test db - Stack Overflow
- swiftui - Previewing SwiftData Records in Xcode Previews - Stack Overflow
- linker - Appending to an ELF file - Cortex MGCC - Stack Overflow
- flutter - Connect user to stripe connect to allow him receiving money - Stack Overflow
- regex - Change text block with nearest search pattern - Stack Overflow
- java - XSLT 3.0 chained burst-streaming with saxon - memory consumption considerations - Stack Overflow
- google cloud platform - Querying public google_ads_transparency_center BQ table doesn't show all the needed data - Stack
- javascript - What are the benefits of Next.js Server Actions, and why should I use them instead of client-side API calls? - Stac
- javascript - <function name> error: FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been
- Page Performance Issue with Countdown in Angular on Component Reload - Stack Overflow