如何在Django模型中为预定义的chocies设置多对多关系?

时间:2022-06-21 20:15:07

I have a WorderOrder class that has predefined work order types:

我有一个具有预定义工作订单类型的WorderOrder类:

class WorkOrder( models.Model ) :
    WORK_TYPE_CHOICES = (
        ( 'hc', 'Heating and cooling' ),
        ( 'el', 'Electrical'          ),
        ( 'pl', 'Plumbing'            ),
        ( 'ap', 'Appliances'          ),
        ( 'pe', 'Pests'               ),
        ( 'ex', 'Exterior'            ),
        ( 'in', 'Interior'            ),
        ( 'ot', 'Others'              ),
    )

    work_type = models.CharField( max_length = 2, choices = WORK_TYPE_CHOICES )
    vendor    = models.ForeignKey( Vendor, null = True, blank = True )

Therefore each order must have one work order type. Later down the road, a vendor can also be assigned to a work order.

因此,每个订单必须具有一个工单类型。在后来的路上,供应商也可以分配到工单。

I want the Vendor class to have a M2M relationship to the same work order choices in the WorkOrder class. In other words, each vendor are able to do one or many work types. For example, Bob's Plumbing can only do "Plumbing", whereas Solid Home Repair can do "Electrical", "Plumbing", "Exterior", and "Interior".

我希望Vendor类与WorkOrder类中的相同工单选项具有M2M关系。换句话说,每个供应商都能够执行一种或多种工作类型。例如,Bob的Plumbing只能做“Plumbing”,而Solid Home Repair可以做“Electrical”,“Plumbing”,“Exterior”和“Interior”。

I understand I can create another table called WorkType and use foreign keys from WorkOrder and a M2M from Vendor, but since I feel I won't be changing the work type choices, I would rather have them predefined in models.py.

我知道我可以创建另一个名为WorkType的表,并使用WorkOrder的外键和供应商的M2M,但由于我觉得我不会改变工作类型的选择,我宁愿在models.py中预先定义它们。

Also if I can predefine it in models.py, then I don't have to pre-populate the table WorkType during deployments and upgrades.

此外,如果我可以在models.py中预定义它,那么我不必在部署和升级期间预先填充表WorkType。

1 个解决方案

#1


1  

Some options for you:

一些选项:

  1. create a model for work_type_choices, instantiate the records (hc, el, etc), then use a manytomany field, or

    为work_type_choices创建一个模型,实例化记录(hc,el等),然后使用manytomany字段,或者

  2. create a charfield and save CSV values to it (eg: "hc, el"), spliting/joining the value into it's elements as required, or

    创建一个字段并将CSV值保存到它(例如:“hc,el”),根据需要将值拆分/加入其元素中,或者

  3. encapsule the above charfield and functions into a custom field and use that

    将上面的字段和函数封装到自定义字段中并使用它

  4. leverage someone else's snippet, eg: http://djangosnippets.org/snippets/1200/

    利用别人的片段,例如:http://djangosnippets.org/snippets/1200/

#1


1  

Some options for you:

一些选项:

  1. create a model for work_type_choices, instantiate the records (hc, el, etc), then use a manytomany field, or

    为work_type_choices创建一个模型,实例化记录(hc,el等),然后使用manytomany字段,或者

  2. create a charfield and save CSV values to it (eg: "hc, el"), spliting/joining the value into it's elements as required, or

    创建一个字段并将CSV值保存到它(例如:“hc,el”),根据需要将值拆分/加入其元素中,或者

  3. encapsule the above charfield and functions into a custom field and use that

    将上面的字段和函数封装到自定义字段中并使用它

  4. leverage someone else's snippet, eg: http://djangosnippets.org/snippets/1200/

    利用别人的片段,例如:http://djangosnippets.org/snippets/1200/