Django Simple Shipping Model正确的方式?

时间:2013-08-05 21:39:10

标签: django django-models

我正在寻找为发货表创建Django模型结构的建议,你能帮忙吗?我想要简单..这是我想要存储的东西:

        | Fedex          | UPS           | USPS
------------------------------------------------------------------
GROUND  | Home Delivery  | Ground        | Parcel Select
3DAY    | Express Saver  | 3 Day Select  | Priority or First Class
2DAY    | 2Day           | 2nd Day Air   | Priority or First Class
etc     | ..             | ..            | ..

我想从这些中创建一个模型,但我觉得我只是制造一团糟,我的想法完全没有了。这是我的想法,我不满意:

class ShippingMethod(models.Mod
    title = models.CharField(
                    max_length = 20,
                    choices = SHIPPING_TITLES, # Just a list of Fedex/UPS etc
                    default = "fedex"
                    )
    service = models.CharField(
                    max_length = 20,
                    choices = SHIPPING_SERVICES, # Just a list of Ground, 2day, etc
                    default = "GROUND"

    # This model is lacking the "Description", eg: "Parcel Select, 2nd Day Air".

我不想制作三个模型

ShippingMethod -- UPS, USPS, Fedex
ShippingType  -- Ground, 2Day
ShippingDescription -- "Parcel Select, 2nd Day Air"

我觉得我刚开始过于复杂的事情......任何建议都会受到赞赏:)

1 个答案:

答案 0 :(得分:2)

我想你会被困三个模特。但是,我建议你制作ShippingMethod(UPS,USPS,FedEx)模型,以及你的ShippingType(地面,2天,3天等),然后第三个模型是一个ShippingMethodType模型,它将其他两个联系起来:

ShippingMethodType(models.Model):
    shipping_method = ForeignKey(ShippingMethod)
    shipping_type = ForeignKey(ShippingType)

通过这种方式,您可以显示ShippingMethodType,以选择方法和类型的组合以及为该出货单调用的内容。我就是这样做的,除了这种可能性之外,我想不出更优雅的方式:

from django.db import models

Shipper(models.Model)
    name = models.CharField(max_length=16)
    .
    .
    ground = models.CharField(max_length=16)
    two_day = models.CharField(max_length=16)
    three_day = models.CharField(max_length=16)
    .
    .
    def shipPackage(self):
        .
        <some default functionality for all shippers>
        .

ShipperUPS(Shipper):
    .
    <any UPS-specific fields>
    .
    .
    def shipPackage(self):
        .
        <UPS-specific functionality>
        super(Shipper, self).shipPackage()

ShipperUSPS(Shipper):
    .
    .
    <overloaded USPS methods>

ShipperFedEx(Shipper):
    .
    .
    <overloaded FedEx methods>

这样的东西可以派上用场,或者可能有点矫枉过正,具体取决于您的规格。但是,我不认为你可以逃避不使用多个模型。