简体   繁体   English

django对象没有属性“ count”

[英]django object has no attribute 'count'

I am learning django / python and I am stuck on an issue. 我正在学习django / python,但遇到了问题。

I have a view: create_document.py in which I want to count the number of name details from a models class: NameDetails . 我有一个视图: create_document.py ,其中我要计算模型类NameDetails中名称详细信息的数量。

I cannot get the correct syntax! 我无法获得正确的语法!

Here is my models.py code: 这是我的models.py代码:

class NameDetails(FillableModelWithLanguageVersion):
    user = models.ForeignKey(User)
    name_details_prefix_title = models.CharField(null=True, blank=True, max_length=25)
    name_details_first_name = models.CharField(null=False, blank=False, max_length=50)
    name_details_middle_name = models.CharField(null=True, blank=True, max_length=100)
    ....

Here is my create_document.py code in which I have a django wizard. 这是我的django向导的create_document.py代码。 I want to make sure the user has more than 1 name before they can create a document: 在创建文档之前,我想确保用户具有多个名称:

from app_name.core.models import NameDetails

class CreateDocumentWizard(SessionWizardView):
    template_name = 'documents/document_create.html'

    form_list = [
        core_forms.CreateDocumentWizardForm01,
        core_forms.CreateDocumentWizardForm02,
        core_forms.CreateDocumentWizardForm03,
        core_forms.CreateDocumentWizardForm04,
    ]

    def get_form_kwargs(self, step=None):
        kwargs = super(CreateDocumentWizard, self).get_form_kwargs(step)
        kwargs.setdefault('user', self.request.user)
        return kwargs

    def get_context_data(self, form, **kwargs):

        name_details_count = NameDetails(user=self.request.user).count()
        if name_details_count < 1:
            return redirect(settings.MENU_DETAIL_LINK_NAME_DETAILS)

When I use the line to determine the count of NameDetails of the user: name_details_count = NameDetails(user=self.request.user).count() , I get the following error: 当我使用该行来确定用户的NameDetails的计数时: name_details_count = NameDetails(user=self.request.user).count() ,出现以下错误:

NameDetails' object has no attribute 'count' NameDetails对象没有属性“ count”

I have tried many permutations, but I am stuck. 我尝试了许多排列,但是被卡住了。

At the moment you're creating a brand new NameDetails instance with request.user as the user. 目前,您正在创建一个以request.user为用户的全新NameDetails实例。 Instead, you should query the database for existing NameDetails for the current user, and count them. 相反,您应该在数据库中查询当前用户的现有NameDetails ,并对其进行计数。 You can query the database through NameDetails.objects : 您可以通过NameDetails.objects查询数据库:

name_details_count = NameDetails.objects.filter(user=self.request.user).count()

在您的get_context_data函数中应该像这样:

name_details_count = NameDetails.objects.filter(user=self.request.user).count()

Each time you need to make a Django QuerySet youhave to go through the object manager objects so : NameDetails.objects. 每次需要创建Django QuerySet时,都必须遍历对象管理器对象NameDetails.objects. in order to make a count you need a list so you use filter : 为了进行计数,您需要一个列表,以便使用filter

name_details_count = NameDetails.objects.filter(user=self.request.user).count()

in case you need one element you use get : 如果需要一个元素,可以使用get

name_details_count = NameDetails.objects.get(user=self.request.user)

for redirection use better HttpResponseRedirect and make sure the link in setting is correct: 为了进行重定向,请使用更好的HttpResponseRedirect并确保设置中的链接正确:

return HttpResponseRedirect(settings.MENU_DETAIL_LINK_NAME_DETAILS)

This is a Python or maybe even more generally, a programming question. 这是一个Python,或者甚至更一般地,是一个编程问题。

Your line: 您的电话:

name_details_count = NameDetails(user=self.request.user).count()

Is Calling the constructor of the NameDetails class and attempting to call a function count that doesn't exist on it (hence the error type). 正在调用NameDetails类的构造函数 ,并尝试调用不存在于其上的函数count (因此为错误类型)。

To get a list of all NameDetails instances with that user, you use: 要获取该用户的所有NameDetails实例的列表 ,请使用:

name_details = NameDetails.objects.filter(user=self.request.user)

And to get the number of items in this list, you can simply use the python built-in len() function like so: 要获得此列表中的项目数,您可以简单地使用python内置的len()函数,如下所示:

name_details_count = len(NameDetails.objects.filter(user=self.request.user))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM