0

Suppose I have two models, one for items-to-be-repaired and other for maintenance-orders. Items-to-be-repaired are created and await for a later assignment to one maintenance-order. One maintenance-order can include several items but each item can only be assigned to a unique maintenance order.

How should I set the models so that I can select multiple items in the create form for the maintenance-order?

I am trying:

class ItemsToBeRepaired(models.Model):  
    maintenance_order = models.ForeignKey(MaintenanceOrder, null=True, blank=True)

class MaintenanceOrder(models.Model):
    pass

Is this correct? How do you include a model field in MaintenanceOrder so that it shows in the CreateView and select multiple items to be repaired?

I apologize if this is a simple question, I am learning programming and Django, have read the documentation and other questions in StackOverflow but still can't figure out the correct way to do this. I appreciate you help.

5
  • I assume you are planning to put more in the MaintenanceOrder than just "pass". Otherwise, yes, that will give you the type of relationship you want.
    – gallen
    Feb 21, 2017 at 15:04
  • Sure. Then every model has its own fields as required by the project. The thing is that with those models I can add the maintenance order to the instance of ItemsToBeRepaired in a CreateView for the latter, but I can't add items to be repaired in a form for an instance of MaintenanceOrder in a CreateView for this one.
    – naccode
    Feb 21, 2017 at 19:34
  • Maybe I need an intermediate model?
    – naccode
    Feb 21, 2017 at 19:42
  • Hmm. To be honest, I do not have much experience with the CreateViews. i would suggest adding the django-views tag to your question and see if it gets more skilled attention. :)
    – gallen
    Feb 21, 2017 at 19:49
  • Your models are correct. To create the form you want, look into inline formsets. Note that the example there exactly matches your scenario. Feb 22, 2017 at 3:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.