Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

'builtin_function_or_method' object has no attribute 'model'

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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>

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading