Saturday, March 26, 2011

Re: RelatedManager - obj.x_set.add(y) doesn't work for me ?

> <snip>
>
> You are a bit confused about ForeignKeys. You have an FK from BookMark to
> User. That means that each BookMark can only have one User, but one user can
> have many bookmarks. That sounds right.
>
> So, there is no such thing as a `user_set` manager for BookMarks. There is
> simply a `user` property, which you set just like any other field:
>
>     b.user = u
>
> If you wanted to add a BookMark to a User, *that's* when you would have a
> RelatedManager:
>
>     u.bookmark_set.add(b)
>

Thank you, that sorted things out.

For anyone else reading this in the future my next question was how to
update the 'tags' property for object instantiated from the BookMark
class.

Well it turns out you can do this ...

#Assuming you have already created two objects
#of the Tag class - t1 and t2

b.tags=(t1,t2)

... and the many-to-many relationship which exists between the tags
property and the Tags model will be maintained as it should be.
There's only thing which is that if you don't do a ...

b.save()

... before attempting the ...

b.tags=(t1,t2)

.... you'll get an error message ...

ValueError: 'BookMark' instance needs to have a primary key value
before a many-to-many relationship can be used

... which is understandable given that the underlying table which
supports the many to many needs to reference the primary key of the
table which underlies the BookMark objects.


Thanks again for your help.


Richard.


> --
> DR.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment