Thanks, That did the trick. I tried to save a client like that but got an error that's why i thought it didn't work like that. didn't pay attention to the _id.
thanks for the help. Richard On Dec 19, 10:17 pm, Jan Rademaker <[EMAIL PROTECTED]> wrote: > On Dec 19, 8:57 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> > wrote: > > > Hello Ben, > > > I'm not really sure what you mean by leave the _id out. > > What i did with the sponsor_id and method_id was getting the correct > > objects from the Method and Sponsor class > > He means there is no reason the append _id to your field names as that > is done by django itself. If you look at the database you'll probably > see that the method table has a method_id_id field. Actually, if > you're naming your autofields something_id it's completely OK to not > define them at all, django will ad d one for you. See my code below. > > > The Client class i believe is a attribute of both ( Sponsor and > > Method ) > > A manager called clients_set is an attribute of both the Sponsor and > Method model, not the Client class itself. > > > > > If you use only one ForeignKey the normal code to add some values > > would be > > > sponsor = Sponsor.objects.get(sponsor_id=1) > > > sponsor contains the Sponsor object for sponsor_id 1 > > next you could add data in clients by > > > sponsor.clients_set.create(labcode=101,invoiceNr='None') > > > The problem is that clients is not only a attribute from sponsor but > > also from method > > so this way of getting values in the clients table won't work. because > > you have to reference both sponsor as method and that gives the > > attributeError > > Mmh, no. You can create clients in one of the following ways (assuming > there is a Method object m and a Sponsor object s): > > #1 (direct way) > > >>> c = Clients(labcode=101, invoiceNr='123', sponsor=s, method=m) > >>> c.save() > > #2 (through a Sponsor object) > > >>> s.clients_set.create(labcode=102, invoiceNr='456', method=m) > > #3 (through a Method object) > > >>> m.clients_set.create(labcode=103, invoiceNr='789', sponsor=s) > > if i would use sql queries i could just add the sponsor and method > > values in the table that are related to the same values in the sponsor > > and method class > > but i'm interested in seeing how Django would deal with this. > > > if anyone knows how to deal with this, i would really appreciate the > > help. > > Here's the model code I used: > > class Method(models.Model): > name = models.CharField(max_length=20) > > class Sponsor(models.Model): > name = models.CharField(max_length=50) > > class Clients(models.Model): > labcode = models.AutoField(primary_key = True) > invoiceNr = models.IntegerField() > sponsor = models.ForeignKey(Sponsor) > method = models.ForeignKey(Method) > > I hope that sheds some light on things. > > > > > Regards, > > > Richard > > > On Dec 19, 6:19 pm, "Ben Ford" <[EMAIL PROTECTED]> wrote: > > > > Try this: > > > > class Clients(models.Model): > > > labcode = models.AutoField(primary_key = True) > > > invoiceNr = models.IntegerField() > > > sponsor = models.ForeignKey(Sponsor) > > > method = models.ForeignKey(SubmitMethod) > > > > sponsor_id is the actual value stored in the db (i.e the pk value of the > > > instance that the ForeignKet points at). If you get rid of the _id from > > > the > > > end of the field name it should work. > > > > Ben > > > > On 19/12/2007, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > > > > Hello Django Users, > > > > > I just started working with django again but i'm running into some > > > > troubles adding values into my database. > > > > just for an example i made the following three tables > > > > > class Method(models.Model): > > > > method_id = models.AutoField(primary_key=True) > > > > methodName = models.CharField(max_length=20) > > > > > class Sponsor(models.Model): > > > > sponsor_id = models.AutoField(primary_key=True) > > > > sponsorName = models.CharField(max_length=50) > > > > > class Clients(models.Model): > > > > labcode = models.AutoField(primary_key = True) > > > > invoiceNr = models.IntegerField() > > > > sponsor_id = models.ForeignKey(Sponsor) > > > > method_id = models.ForeignKey(SubmitMethod) > > > > > I added some values into the tables method and sponsor and now i want > > > > to add the values into the table clients. The problem is that it has > > > > multiple foreignkeys and the method from the tutorial doesn't work in > > > > this case > > > > > sponsor = Sponsor.objects.get(sponsor_id=1) > > > > method = Method.objects.get(method_id=1) > > > > > sponsor.method.clients_set.create(labcode=101,invoiceNr='None') > > > > > the error it states is the following > > > > AttributeError: 'Sponsor' object has no attribute 'method' > > > > > offcourse that is correct because only clients is a attribute from > > > > sponsor. > > > > > Can someone help me out, how can i add values to this table using the > > > > django method ? ( offcourse i can fall back to sql querying but i like > > > > to know the django way ) > > > > > any help would be greatly appreciated, > > > > > Regards, > > > > > Richard M > > > > -- > > > Regards, > > > Ben Ford > > > [EMAIL PROTECTED] > > > +6281317958862 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---