一尘不染

“builtin_function_or_method”对象没有属性“模型”

py

我按类别排序,启动服务器时出现此错误。如何解决?

error
图片:https ://i.stack.imgur.com/W4ROr.png

filter/views.py

from django.shortcuts import render
from .filters import *
from .forms import *

# Create your views here.
def filter(request):
    index_filter = IndexFilter(request.GET, queryset=all)
    context = {
        'index_filters': index_filter,
    }
    return render(request, 'filter.html', context)

filter/filters.py

import django_filters
from search.models import *

class IndexFilter(django_filters.FilterSet):
    class Meta:
        model = Post
        fields = {'brand'}

search/models.py

from django.db import models

class Post(models.Model):
    BRAND = [
        ('apple', 'apple'),
        ('samsung', 'samsung'),
        ('huawei', 'huawei'),
        ('nokia', 'nokia'),
    ]
    img = models.URLField(default='https://omsk.imperiya-pola.ru/img/nophoto.jpg')
    title = models.CharField(max_length=80)
    brand = models.CharField(max_length=20, choices=BRAND)
    content = models.TextField()
    price = models.FloatField(default=1.0)

    def __str__(self):
        return self.title

filter/urls.py

from django.urls import path
from .views import *
urlpatterns = [
    path('filter/', filter, name='filter')
]

templates/filter.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form method="get" action="{% url 'index' %}">
        {{ index_filters.form }}
        <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
</body>
</html>

阅读 118

收藏
2022-09-30

共1个答案

一尘不染

all是python中的内置函数。因此,当您将其分配给查询集时,django 会引发错误,因为该all函数没有属性model

index_filter = IndexFilter(request.GET, queryset=all)

解决方案是为您的查询集使用不同的名称。

2022-09-30