选择字段取决于当前类的count()

时间:2011-04-05 15:01:13

标签: django model

我想在一个充满数字的类中添加一个选择字段。数字的数量必须是Object.objects.count()的总数。

我想这样做:

class Category(models.Model):

    quantity = Category.objects.count() + 1
    PRIORITY = tuple((str(n), str(n)) for n in range(aantal + 1))

但后来我收到了这个错误:

NameError: name 'Category' is not defined

2 个答案:

答案 0 :(得分:0)

我假设您希望在表单中提供这些选项,因此最好的选择是使用IntegerField,然后阅读http://docs.djangoproject.com/en/dev/topics/forms/modelforms/并根据该表单创建表单并更改 init ()(如图here

答案 1 :(得分:0)

要避免使用name error,您应该为模型创建专用方法来计算类别:

def total_categories(self):
    return Category.objects.count()
相关问题