私はショッピングカートアプリの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.ビジョン', 'ビジョン'))
選択肢を入力するために呼び出されたとき、最初の選択肢、つまり最初のオプションを失いました。説明が必要です。