You can always just pass the id of A into the template for creating the B and have it submitted along with the form. Then after you save the B you will have to get the A and save the instance of B to its relation. -richard
On May 17, 2008, at 3:02 AM, jabbercat wrote: > > Hello, > > the answer for question 1 I already find out, thank you. > > According to question 2, I'm not meaning without a relation between A > and B. There must be a relation between them. So I describe my problem > again in a more understandable way, I hope. > > I have two models A and B with an one(A)-to-many(B) relationship. > > So I want to build a view for adding a B, which is related to a A. For > that I have a link in another view. That Link contains the ID of A. So > in the new view I knew the ID of A, which should be related with the > new B. How I get B related with A? (Ok, by setting the attribute, > but...) How can I do that by using form_for_model() according to my > database model? The User don't have the choice to manipulate the ID of > A or something like that. There shouldn't be a field for the ID! > > On 16 Mai, 22:52, "Richard Dahl" <[EMAIL PROTECTED]> wrote: >> For the first question, the db-api doc should provide this and more >> information, as I am not sure I completely understand, but here goes >> (assuming the following models): >> >> class B(models.Model): >> attribute = char()... >> >> class A(models.Model): >> attribute = char()... >> bs = m2m(B) >> >> in your view to get all of the B(s) related to a given A >> >> a = A.objects.get(pk=1) >> b = a.bs.all() >> >> if you want to get all of the Bs that are related to 'any' A: >> b = B.objects.filter(a__isnull = False) >> >> for question 2, I am not sure what you mean, if you mean can you >> create a B >> without relating it to an A? sure, a related A is not required by >> B. If you >> do not want to require a B for A just set 'blank=True, null=True' >> in the M2M >> field definition in A. >> >> Not sure if this helps, your original post was somewhat confusing, >> if you >> post relevant snippets from your models you may get more intelligent >> responses than my intellectual meanderings. >> -richard >> >> On 5/16/08, jabbercat <[EMAIL PROTECTED]> wrote: >> >> >> >> >> >> >> >>> Hello, >> >>> it's my frist time I'm using this Group. >> >>> My Problems: >>> I have a class A and a class B with a one(A)-to-many(B) relationship >>> between them. >> >>> My first question: Is there an efficient way to get in a template >>> all >>> instances of B, which are related to an instance of A, when i only >>> knew an instance of A? Something like a special attribute, or >>> something like that? >> >>> My second question: I want to use form_for_model(), because of my >>> database-driver design. But how can I add an instance of B so that >>> the >>> relation to an instance of A is also saved, without specify the >>> foreign key id of A explicitly? >> >>> I hope you understand what I meen. > > --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---