Frage

I am reading a book called 'Packt Publishing, Learning Website Development with Django' and I am doing a tutorial on how to create a bookmarking website where users can read articles and bookmark them / save the url to the article. Here is the models.py

from django.db import models
from django.contrib.auth.models import User
# Create your models here.

class Link(models.Model):
    url = models.URLField(unique=True)

class Bookmark(models.Model):
    title = models.CharField(max_length=200)
    user = models.ForeignKey(User)
    link = models.ForeignKey(Link)

Now, this is my user_page view (where the username is passed as a parameter to the view)

def user_page(request, username):
    try:
        user = User.objects.get(uesrname=username)
    except:
        raise Http404('Requested user not found.')

    bookmarks = user.bookmark_set.all()
    variables = {
        'username': username,
        'bookmarks': bookmarks
    }

    return render(request, 'user_page.html', variables)

I'm confused about the line

bookmarks = user.bookmark_set.all()

I understand that 'user' is a user object, but we are using the generic User model provided by Django, which only has username, password and email, right? So where is bookmark_set coming from? Because if it is trying to access all bookmarks from the bookmark class, shouldn't the 'b' in bookmark_set at least be upper case? and what does the _set do? In the book, it explains this line by saying

"To obtain the list of bookmarks for a particular user object, we can conveniently use the bookmark_set attribute available in the user object"

I tried google'ing all the attributes available for the user object but couldn't find anything. Is there a website which has a list of all the attributes available for the user object?

War es hilfreich?

Lösung

The bookmark_set comes from the Bookmark definition and more specifically, from the line

user = models.ForeignKey(User)

This line creates a user attributes to the Bookmark model but also adds a bookamrk_set attribute to the User model!

This is explained in the django documentation. For completeness, I'm copying from the docs:

Django also creates API accessors for the “other” side of the relationship – the link from the related model to the model that defines the relationship. For example, a Blog object b has access to a list of all related Entry objects via the entry_set attribute: b.entry_set.all().

Andere Tipps

bookmark_set is the reverse/"other side" of the ForeignKey. You have a FK from Bookmark to User, so Django automatically generates a bookmark_set on the User.

See also the official Django documentation: Following relationsips "backward"

You should have done the Django tutorial, where this is explained.

bookmark_set is the reverse relation for the ForeignKey that's defined in Bookmark and points to User. Every foreign key in Django gets this reverse accessor, which in this case gives you all bookmarks related to the particular User object you have.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top