# Type object has no attribute objects on view

**URL:** <https://forum.djangoproject.com/t/type-object-has-no-attribute-objects-on-view/36032>\
**Category:** Mystery Errors\
**Created:** [October 29, 2024, 2:23am UTC](https://forum.djangoproject.com/t/type-object-has-no-attribute-objects-on-view/36032 "2024-10-29T02:23:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lsaavedra21](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/lsaavedra21/32/24443_2.png) [@lsaavedra21](https://forum.djangoproject.com/u/lsaavedra21)\
**Post date:** [October 29, 2024, 2:23am UTC](https://forum.djangoproject.com/t/type-object-has-no-attribute-objects-on-view/36032/1 "2024-10-29T02:23:02Z")

</div>

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](https://docs.djangoproject.com/en/4.2/topics/db/models/#model-attributes)  
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

```auto
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

```auto
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

```auto
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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/4/9/49039ed3b5e810415aec00c5cf1c89bf2eae501a.png)

this is the image of the error

 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/4/4/44caada23a5d713797dbc184dfbee798ae5fff0a.png)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 29, 2024, 3:11am UTC](https://forum.djangoproject.com/t/type-object-has-no-attribute-objects-on-view/36032/2 "2024-10-29T03:11:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 29, 2024, 3:20am UTC](https://forum.djangoproject.com/t/type-object-has-no-attribute-objects-on-view/36032/3 "2024-10-29T03:20:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lsaavedra21](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/lsaavedra21/32/24443_2.png) [@lsaavedra21](https://forum.djangoproject.com/u/lsaavedra21)\
**Post date:** [October 29, 2024, 11:51am UTC](https://forum.djangoproject.com/t/type-object-has-no-attribute-objects-on-view/36032/4 "2024-10-29T11:51:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lsaavedra21](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/lsaavedra21/32/24443_2.png) [@lsaavedra21](https://forum.djangoproject.com/u/lsaavedra21)\
**Post date:** [October 29, 2024, 12:01pm UTC](https://forum.djangoproject.com/t/type-object-has-no-attribute-objects-on-view/36032/5 "2024-10-29T12:01:08Z")

</div>

Hi @KenWhitesell

😮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.
