0

私はショッピングカートアプリのmodels.pyにこれを持っています:

def get_all_models():
    tup = []
    for ct in ContentType.objects.filter(app_label__in=['shoppingcart','products','productoptions']):
        if ct is not None:
            mclass = ct.model_class()
            if mclass is not None:
                tup.append((mclass.__module__+'.'+mclass.__name__,mclass.__name__))
    return tuple(tup)

class ConditionSet(models.Model):
    model_name      = models.CharField(max_length=50, choices = get_all_models()) 
    model_attribute = models.CharField(max_length=50)
    operator        = models.CharField(max_length=50, choices=OPERATORS)
    val             = models.CharField(max_length=50,null=True,blank=True)
    evaluation      = models.NullBooleanField(null=True, blank=True)
    def get_all_models():
        tup = []
        for ct in ContentType.objects.filter(app_label__in=['shoppingcart','products','productoptions']):
            if ct is not None:
                mclass = ct.model_class()
                if mclass is not None:
                    tup.append((mclass.__module__+'.'+mclass.__name__,mclass.__name__))
        return tuple(tup)
    def evaluate(self):
        app_label = str(self.model_name.split('.')[0])
        model_name = str(self.model_name.split('.')[2])
        model = get_model(app_label= app_label, model_name = model_name)
    def __str__(self):
        return self.model_attribute

admin.py では、モデルのみを登録しました

def get_all_models() が2つの異なる出力セットを生成することがわかりました。シェルから実行すると、これがあります

get_all_models() (('shoppingcart.models.Cart', 'Cart'), ('shoppingcart.models.CartItem', 'CartItem'), ('shoppingcart.models.CartRule', 'CartRule'), ('products. models.CasesAccessory', 'CasesAccessory'), ('productoptions.models.Coating', 'Coating'), ('shoppingcart.models.ConditionSet', 'ConditionSet'), ('products.models.Eyeglass', 'Eyeglass' ), ('products.models.GiftVoucher', 'GiftVoucher'), ('productoptions.models.Lens', 'Lens'), ('productoptions.models.Prescription', 'Prescription'), ('products.models. Readingglass', 'Readingglass'), ('products.models.Sunglass', 'Sunglass'), ('productoptions.models.Tint', 'Tint'), ('productoptions.models.ビジョン', 'ビジョン'))

選択肢を入力するために呼び出されたとき、最初の選択肢、つまり最初のオプションを失いました。説明が必要です。

4

1 に答える 1

2

Django がモデルを検証すると、コードが実行されます。したがって、Cartおそらくまだ検証されておらず、そのモデル クラスはNoneです。

モデル定義で選択肢を設定しないことをお勧めします。以前にいくつか問題がありました。代わりに、フォームをインスタンス化するときなど、後で動的に入力することをお勧めします。

于 2013-02-11T13:59:33.620 に答える