Home > OS >  'builtin_function_or_method' object has no attribute 'model'
'builtin_function_or_method' object has no attribute 'model'

Time:09-30

I'm sorting by category and I get this error when starting the server. How to solve it?

Error
Image: 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  type="submit">Search</button>
    </form>
</body>
</html>

CodePudding user response:

all is a builtin function in python. so when you assign it to the queryset django throws an error, because the all function has no attribute model.

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

The solution would be to use a different name for your queryset.

  • Related