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!
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=.