Help a newbie with his simple foreignkey problem?

0 views
Skip to first unread message

Cody Django

unread,
Nov 12, 2009, 1:23:29 PM11/12/09
to Django users
Hi - I'm having a hard time wrapping my head around one-to-many
relations. I'd like to be able to do this in a view:

b = Book.objects.get(id=1) #new book
request.user.get_profile().shelf1.add(b) #add book to shelf1
request.user.shelf1.remove(b) #remove from shelf1
request.user.shelf2.add(b) #add to shelf2

Both shelves are exactly the same, and each user always only has two
shelves. The shelf objects are create and assigned to the UserProfile
variables upon creation of the UserProfile. I figure the models
should be organized like so:

Class Book(models.Model)
#book fields

Class Shelf(models.Model)
books = models.foreignkey(book)
def add(self, b):
self.books.append(b) #not sure about this
def remove(self, Book):
self.books.remove(b) #not sure about this

Class UserProfile(models.Model):
shelf1 = models.foreignkey(Shelf)
shelf2 = models.foreignkey(Shelf)

I don't understand how to add and remove objects from a one-to-many
relation. According to the docs, going backward through a foreignkey
relation makes available queryset methods such as .add, .clear,
and .remove - but there aren't similar methods for going forward?

Thank you for your insight!

R. Gorman

unread,
Nov 12, 2009, 2:09:39 PM11/12/09
to Django users
> b = Book.objects.get(id=1)  #new book
> request.user.get_profile().shelf1.add(b)  #add book to shelf1
> request.user.shelf1.remove(b)     #remove from shelf1
> request.user.shelf2.add(b)      #add to shelf2

Looks like you're trying to add the foreignkey to the User model
instead of the UserProfile model. If you go:

request.user.userprofile_set.add(b)

You should have the object added. You can confirm that the userprofile
model is associated with the User model in the python shell using the
command dir(User) after you've imported the User model. You should see
'userprofile_set'. Or you could user:

p = request.user.get_profile()
p.shelf1.add(b)

R.

Cody Django

unread,
Nov 12, 2009, 4:02:32 PM11/12/09
to Django users
Sorry - it should read:

b = Book.objects.get(id=1) #new book
request.user.get_profile().shelf1.add(b) #add book to shelf1
request.user.get_profile().shelf1.remove(b) #remove from shelf1
request.user.get_profile().shelf2.add(b) #add to shelf2

I think something like this works:

Class Book
#details
Class Shelf1
user = foreignkey(UserProfile)
book = foreignkey(related_name ="shelf1")
Class Shelf2
user = foreignkey(UserProfile)
book = foreignkey(related_name="shelf2")
Class UserProfile
user = foreignkey(User, unique = True)

but it doesn't look very nice... I'd prefer to find a way to do
something like this:

b = Book.objects.get(id=1) #new book
request.user.get_profile().shelf.1.add(b) #add book to shelf1
request.user.get_profile().shelf.1.remove(b) #remove from shelf1
request.user.get_profile().shelf.2.add(b) #add to shelf2

Reply all
Reply to author
Forward
0 new messages