foreign key change depending on previous selection Django or Dynamic Foreign key

76 views
Skip to first unread message

jayson lauwerends

unread,
Jun 1, 2022, 6:35:54 PM6/1/22
to Django users

Hello everyone i have quick question with something i am having trouble with. i have these two model which use foreign keys. but i want one of the foreign keys options to change depending on what you selected in the previous fk.

i also posted this on stack overflow here is the link:
https://stackoverflow.com/questions/72465024/foreign-key-change-depending-on-previous-selection-django

Here is the code for my models.

class Customer(models.Model):

company_name = models.CharField(max_length=255)
contact_person = models.CharField(max_length=255)
email = models.EmailField() phone = models.IntegerField()
..............

Connections(models.Model):

company = models.ForeignKey(Customer, on_delete=models.CASCADE)
customerVlan = models.ForeignKey(CustomerVlan, on_delete=models.CASCADE) switch_port_number = models.IntegerField(default=0)
duplex = models.CharField(choices=duplex, max_length=200)
............

class WorkOrder(models.Model): work_order_date = models.DateTimeField(default=timezone.now)
request_date = models.DateField(default=timezone.now)
company_id = models.ForeignKey(Customer, on_delete=models.CASCADE)
connections= models.ForeignKey(Connections, on_delete=models.CASCADE, blank=True, null=True)
.. ..........

Screenshot 2022-06-01 120740.png

as you can see in the image when i select a company_id i want only connections with that company to show up and not everything in the database because there are allot of connections.

Reply all
Reply to author
Forward
0 new messages