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
- [Answered ]-Django: cant import model to a .py on the same path as models
- [Answered ]-How to Send Asynchronous Emails In Django Using Celery?
- [Answered ]-Require the user to change their password on first login?
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
Source:stackexchange.com