Type object has no attribute objects on view

Hi team

I am facing with this error: Type object “Factory” has no attribute objects.
As I read this is related to model attribute issue, I meant it is related to an code error I have related to model attribute. I tried to undertood what the is the issue reading - Models | Django documentation | Django
But I was not able.
the issue is shown on web page when I tried to get the html template related to Factory model.

I have project create “PlatecAdmin” and inside of it I have an app named “platec” so all the files for model, view, admin are located inside the app folder “platec”

Model.py

from django.db import models
from django.conf import settings
from django.contrib.auth.models import User

# Create your models here.

class IsActive(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(is_active=True)
    


class SoftDelete(models.Model):
    is_active = models.BooleanField(default=True, null=False, blank=False)
    
    all_records = models.Manager()
    active_records = IsActive()

    def soft_delete(self):
        self.is_active = False
        self.save()

    def undelete(self):
        self.is_active = True
        self.save()

    class Meta:
        abstract = True



class SoftDeleteManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(is_active = True)



class Factory(SoftDelete):
    factory_id = models.AutoField(primary_key=True, null=False, blank=False)
    factory_cd = models.CharField(max_length=3, null=False, blank=False)
    factory_name = models.CharField(max_length=100, null=False, blank=False)
    factory_desc = models.CharField(max_length=255, null=True, blank=True)
    comments = models.CharField(max_length=300, null=True, blank=True)
    created_dt = models.DateField(auto_now_add=True, null=False, blank=False)
    created_by = models.ForeignKey(User, on_delete=models.PROTECT, null=False, blank=False, related_name='cr_factory')
    last_modify_dt = models.DateField(auto_now=True, null=False, blank=False)
    last_modify_by = models.ForeignKey(User, on_delete=models.PROTECT, null=False, blank=False, related_name='lm_factory')
    
    def __str__(self) -> str:
        return f"{self.factory_id} - {self.factory_cd} - {self.factory_name} - {self.factory_desc} - {self.comments} - {self.created_dt} - {self.created_by} - {self.last_modify_dt} - {self.is_active}"
    


class Setor(SoftDelete):
    setor_id = models.AutoField(primary_key=True, null=False, blank=False)
    setor_cd = models.CharField(max_length=3, null=False, blank=False)
    setor_name = models.CharField(max_length=100, null=False, blank=False)
    setor_desc = models.CharField(max_length=255, null=True, blank=True)
    comments = models.CharField(max_length=300, null=True, blank=True)
    created_dt = models.DateField(auto_now_add=True, null=False, blank=False)
    created_by = models.ForeignKey(User, on_delete=models.PROTECT, null=False, blank=False, related_name='scr_factory')
    last_modify_dt = models.DateField(auto_now=True, null=False, blank=False)
    last_modify_by = models.ForeignKey(User, on_delete=models.PROTECT, null=False, blank=False, related_name='slm_factory')
    
    def __str__(self) -> str:
        return f"{self.setor_id} - {self.setor_cd} - {self.setor_name} - {self.setor_desc} - {self.comments}"
    

class factory_setor(SoftDelete):
    fac_set_id = models.AutoField(primary_key=True, null=False, blank=False)
    factory = models.ForeignKey(Factory, on_delete=models.PROTECT)
    setor = models.ForeignKey(Setor, on_delete=models.PROTECT)
    created_dt = models.DateField(auto_now_add=True, null=False, blank=False)
    created_by = models.ForeignKey(User, on_delete=models.PROTECT, null=False, blank=False, related_name='fscr_factory')
    last_modify_dt = models.DateField(auto_now=True, null=False, blank=False)
    last_modify_by = models.ForeignKey(User, on_delete=models.PROTECT, null=False, blank=False, related_name='fslm_factory')
    
    def __str__(self) -> str:
        return f"{self.fac_set_id} - {self.factory} - {self.setor}"
    

Admin.py

from django.contrib import admin
from django.forms import ModelForm
from django.http import HttpRequest
from .models import Factory, factory_setor, Setor


@admin.register(Factory)
class FactoryAdmin(admin.ModelAdmin):

    list_display = ('factory_id', 'factory_cd', 'factory_name', 'factory_desc', 'comments')
    list_filter = ('is_active', 'factory_name')
    search_fields = ('factory_cd' 'factory_name',)
    actions = ['restore_selected', 'inactivate_records']

    def restore_selected(modelAdmin, request, queryset):
        queryset.update(is_active=True)

    
    def inactivate_records(modelAdmin, request, queryset):
        queryset.update(is_active=False)

    
    def has_delete_permission(self, request, obj=None):
        return False
    

    def save_model(self, request, obj, form, change):

        if not change:
            obj.created_by = request.user
            obj.last_modify_by = request.user

        else:
            obj.last_modify_by = request.user

        return super().save_model(request, obj, form, change)
    
    restore_selected.short_description = 'Activate selected Records'
    inactivate_records.short_description = 'Inactivate selected Records'

    exclude = ['created_by', 'last_modify_by']


View.py

from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from .models import Factory, factory_setor, Setor


def platec(request):
    template = loader.get_template('test.html')
    return HttpResponse(template.render())


def factory(request):
    myfactories = Factory.objects.all().values()
    template = loader.get_template('factories.html')

    context = {
        'myfactories': myfactories,
    }

    return HttpResponse(template.render(context, request))

this is the folder schema of my project

this is the image of the error

Note: When requesting assistance with an error message, please post the complete error with the full traceback from the server console. Do not bother posting a screenshot of the browser window.

Your default manager for Factory is not named “objects”. Since Factory is a subclass of an abstract class (SoftDelete), it inherits the manager definition for all_records, which becomes the default Manager.

Hi @KenWhitesell
Dully noted, will keep it in mind for next time.

Hi @KenWhitesell

:open_mouth:Oh! I think I got it,
that means for this case on my view instead of call the Factory.objects as I did
myfactories = Factory.objects.all().values()

I should call the all_records, so it should be something like: myfactories = Factory.all_records()

Thank you, I will test it later today.