[Answered ]-Django : AttributeError

1πŸ‘

βœ…

The main issue here is you need to use the correct related name format, which is all lowercase; but there are some further issues with your models.

Clearning up your code, you end up with this:

from django.shortcuts import get_object_or_404, render

def user_page(request, username):
    u = get_object_or_404(User, username=username)
    feeds = u.userfeed_set.all()
    variables = {
        'username' : username,
        'feeds' : feeds
    }
    return render(request, 'user_page.html', variables)

Your models also need some editing, because you have no self.link.url:

from django.contrib.auth.models import User

class AllFeeds(models.Model):
    url = models.URLField(unique=True, max_length=40)

    def __unicode__(self):
        return unicode(self.url)

class UserFeed(models.Model):
    user = models.ForeignKey(User)
    myfeeds = models.ForeignKey(AllFeeds)

    def __unicode__(self):
        return unicode('{0} {1}'.format(self.user.username, self.myfeeds.url))
πŸ‘€Burhan Khalid

1πŸ‘

If you haven’t a related_name on the ForeignKey field of the UserFeed model, it should be a accessible as user.userfeed_set. (The default name is model.__name__.lower() + "_set").

If you have set the releated_name, the method will be called whatever you named gave as the value.

πŸ‘€DanielB

0πŸ‘

as I know, if you have the two models using manytomany relation, you can use u.userfeed_set.all(), but as u said, your models are using one-to-one relation

πŸ‘€user1564928

Leave a comment