'User' object has no attribute 'items'

Helo, am trying to get this logic that a user can have many images. User is related to the Items in model and i keep getting this error: What am i doing wrong?

this is the views

def index(request):
    users = request.user.items
    img = users.images.all().count()
    item = Items.objects.filter()

  context = {
      'user':users,
      'imgs':img,
      'items':item,
  }
  return render(request, 'jijiapp/index.html', context)

This is the model

class Items(models.Model):
    user = models.ForeignKey(User, on_delete = models.CASCADE, null=True, blank =True)
    title = models.CharField(max_length=10, null=True, blank =True)
    images = models.ManyToManyField('Images')
    post_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

    class Meta:
        ordering =['title',]
        # verbos_name ='Items'

  class Images(models.Model):
      image =models.ImageField(upload_to='media/', null=True, blank=True)
  
      def __str__(self):
          return str(self.image)

This is the Traceback:

System check identified no issues (0 silenced).
January 18, 2023 - 23:07:41
Django version 4.0, using settings 'jijiclone.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Internal Server Error: /home/
Traceback (most recent call last):
  File "C:\Users\Christopher\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Christopher\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Christopher\jijiclone\jijiapp\views.py", line 6, in index
    users = request.user.items
  File "C:\Users\Christopher\AppData\Local\Programs\Python\Python38\lib\site-packages\django\utils\functional.py", line 249, in inner
    return func(self._wrapped, *args)
AttributeError: 'User' object has no attribute 'items'
[18/Jan/2023 23:08:21] "GET /home/ HTTP/1.1" 500 66051

If you want to access the items with the exact property name ”items”, you should define a related_name in your Foreign Key to the User model named “items”, see: Model field reference | Django documentation | Django

Otherwise you can also access the reverse Foreign Key with the auto generated property request.user.item_set. (Don’t forget to add .all() to perform the actual query).

Thank you so much maerteijn. It realy worked. I have added the related name as follows

class Items(models.Model):
    user = models.ForeignKey(User, on_delete = models.CASCADE, related_name='useritem', null =True)
    title = models.CharField(max_length=10, null=True, blank =True)
    images = models.ManyToManyField('Images')
    post_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

    class Meta:
        ordering =['title',]
        # verbos_name ='Items'

and the view

  def index(request):
      users = request.user.useritem
      img = users.images.all().count()
      item = Items.objects.all()
  
      context = {
          'user':users,
          'imgs':img,
          'items':item,
      }
      return render(request, 'jijiapp/index.html', context)

But now am getting this attributeerrow

  AttributeError: 'RelatedManager' object has no attribute 'images'

You should read the docs at Related objects reference | Django documentation | Django and Many-to-many relationships | Django documentation | Django to understand the usage of the related name reference for a foreign key. (A many-to-many relationship works the same way as the reverse reference to a foreign key which is why that second link is appropriate.)

You might also want to review the work you would have done in the Official Django Tutorial in the section Playing with the api to see examples at work.