0

Hello SO,

I just created two models:

models.py

from django.db import models
from django.contrib.auth.models import User
import uuid
from uuid_upload_path import upload_to
from django.core.exceptions import ValidationError
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _


class StorageObject(models.Model):
    name = models.CharField(max_length=200)
    text = models.TextField(blank=True)
    slug = models.UUIDField(default=uuid.uuid4)
    display = models.BooleanField(default=True)
    owner = models.ForeignKey(User, on_delete=models.CASCADE)
    # types
    file = models.FileField(upload_to=upload_to, blank=True, null=True)
    image = models.ImageField(upload_to=upload_to, blank=True, null=True)
    link = models.URLField(blank=True)

    @cached_property
    def file_type(self):
        if self.file:
            return 1
        elif self.image:
            return 2
        elif self.link:
            return 3

    def clean(self):
        if self.file and (self.image or self.link):
            raise ValidationError(_('Storage Objects can only contain one storage type each'))
        elif self.image and (self.link or self.file):
            raise ValidationError(_('Storage Objects can only contain one storage type each'))
        elif self.link and (self.file or self.image):
            raise ValidationError(_('Storage Objects can only contain one storage type each'))
        if not (self.link or self.image or self.file):
            raise ValidationError(_('Storage Objects must contain exactly one storage type'))

    def __str__(self):
        return '%s (by %s)' % (self.name, self.owner)


class Collection(models.Model):
    name = models.CharField(max_length=200)
    active = models.BooleanField(default=True)
    objects = models.ManyToManyField(StorageObject)
    owner = models.ForeignKey(User, on_delete=models.CASCADE)

As the title says I´m getting an AttributeError whenever I am trying to create a Collection via admin-interface. However creating the StorageObject works perfectly fine and gets propperly cleaned.

I googled about this message, and most people seem to have forgotten the instance brackets (like StorageObject() ) somewhere. Maybe I am blind, but I just can´ t find the mistake.

So I went on and deleted my custom function file_type and clean, but still the same result.

What am I missing here? Thanks for your time!

Stack

 File "C:\Users\[..]\venv\lib\site-packages\django\db\models\manager.py", line 176, in __get__
        raise AttributeError("Manager isn't accessible via %s instances" % cls.__name__)
    AttributeError: Manager isn't accessible via Collection instances
errorinpersona
  • 380
  • 5
  • 17

1 Answers1

1

Don't add a field objects on your models, this is conflicting with the model Manager that's also called objects. Collection.objects should return a Manager unless you decide to give it a different name (see here).

dirkgroten
  • 20,112
  • 2
  • 29
  • 42