My knowledge of django is less than a week old, so I’m sure my question is out of my pure inability to process enough information quickly enough…
I am building a bus/vehicle fleet management system for the school that I work at. I understand how to and have implemented the one to many relationships from my primary fleet model to several other models using foreign keys (engine type, fuel type, chassis, etc…), but these have all been models that only needed 1 field.
I am trying to do this all in the admin interface because it is just sooo good, and feels very comfortable, but I may have hit a snag…
I am now to the workorder model, which should also be a many to one relationship. There should be one vehicle (from the fleet model) assigned to each of the workorders contained within the workorder model. This relationship makes just as much sense as the others, but I don’t understand how to fully incorporate the fields from this workorder model into the fieldset of my fleet model. At the moment, I have just the singular foreignkey field, which acts as a selector, just like the other ones, but I also need to be able to modify other elements of the workorder model, preferably both inside of the workorder model directly and within the fleet model.
I thought I could pull this off using the related_names tool, but, while I can add multiple foreignkeys to the same model that way, they just seem to point at the same select option. I haven’t been able to figure out how to phrase my question well enough for Google to help me out:
Is it possible to point to a specific field of a model when it is called using the foreignkey fieldtype? If so, how would I go about doing that?
For clarification, I have the following two models:
name = models.CharField(max_length=100)
active = models.BooleanField()
plate = models.CharField(max_length=20, blank=True, null=True)
vin = models.CharField(max_length=17, blank=True, null=True)
workorder = models.ForeignKey('Workorders', on_delete=models.SET_NULL, blank=True, null=True)
unit = models.ForeignKey(Fleet, on_delete=models.DO_NOTHING, blank=False, null=False, related_name='unit', verbose_name='Vehicle')
vin = models.ForeignKey(Fleet, on_delete=models.SET_NULL, blank=True, null=True, related_name='vin_number')
description = models.TextField(blank=True, null=True, verbose_name='Problem/Description')
I’ve removed several of the other fields for the sake of easier reading…
I guess my questions are as follows:
- How would I pull both the unit and the description fields from Workorders into a Fleet fieldset?
- Is there a way to automatically populate the other foreignkey values of the same model with the information from the model they are referencing? For example, when a user selects a unit on the Workorders form (from either the Fleet fieldset or the Workorders Modle), can the vin field be autopopulated? Maybe even as readonly?
Very sorry for the questions this early in my django adventure. Hopefully this is clear enough to be pointed in the correct direction.
Thanks for your time!