django - Multipe one-to-many relation between two models - Stack Overflow

时间: 2025-01-06 admin 业界

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
Add a comment  | 

1 Answer 1

Reset to default 0

Will 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 ForeignKeys with a given equivalent related_query_name=….


Note: The related_name=… parameter [Django-doc] is the name of the relation in reverse, so from the Destination model to the Vehicle 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 the startdestination relation to vehicles_started.