List Info

Thread: Models with multiple foreignkeys ( adding values )




Models with multiple foreignkeys ( adding values )
user name
2007-12-19 10:55:42
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='Non
e')

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




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


Re: Models with multiple foreignkeys ( adding values )
user name
2007-12-19 11:19:45
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, mendes.richardgmail.com">mendes.richardgmail.com < mendes150gmail.com">mendes150gmail.com> 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):
 &nbsp; &nbsp; &nbsp; &nbsp;method_id = models.AutoField(primary_key=True)
 &nbsp; &nbsp;   ; methodName = models.CharField(max_length=20)

class Sponsor(models.Model):
 &nbsp; &nbsp; &nbsp;  sponsor_id = models.AutoField(primary_key=True)
 &nbsp; &nbsp;   ; sponsorName = models.CharField(max_length=50)

class Clients(models.Model):
 &nbsp;   ; &nbsp; labcode = models.AutoField(primary_key = True)
&nbsp; &nbsp; &nbsp; &nbsp; invoiceNr = models.IntegerField()
 &nbsp; &nbsp; &nbsp; &nbsp;sponsor_id = models.ForeignKey(Sponsor)
 ; &nbsp; &nbsp; &nbsp; 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

&nbsp; &nbsp; &nbsp; &nbsp;   ;sponsor = Sponsor.objects.get(sponsor_id=1)
 &nbsp;   ; &nbsp; &nbsp; 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
ben.fordnzgmail.com">ben.fordnzgmail.com
+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-usersgooglegroups.com
To unsubscribe from this group, send email to django-users-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Re: Models with multiple foreignkeys ( adding values )
user name
2007-12-19 13:57:09
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

The Client class i believe is a attribute of both ( Sponsor
and
Method )
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

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.

Regards,

Richard



On Dec 19, 6:19 pm, "Ben Ford" <ben.for...gmail.com> 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, mendes.rich...gmail.com <mendes...gmail.com> 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='Non
e')
>
> > 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
> ben.for...gmail.com
> +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-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Models with multiple foreignkeys ( adding values )
user name
2007-12-19 15:17:23


On Dec 19, 8:57 pm, "mendes.rich...gmail.com" <mendes...gmail.com>
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"
<ben.for...gmail.com> 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, mendes.rich...gmail.com
<mendes...gmail.com> 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='Non
e')
>
> > > 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
> > ben.for...gmail.com
> > +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-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Models with multiple foreignkeys ( adding values )
user name
2007-12-19 15:52:16
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 <j.radema...gmail.com> wrote:
> On Dec 19, 8:57 pm, "mendes.rich...gmail.com" <mendes...gmail.com>
> 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"
<ben.for...gmail.com> 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, mendes.rich...gmail.com
<mendes...gmail.com> 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='Non
e')
>
> > > > 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
> > > ben.for...gmail.com
> > > +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-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-5]

about | contact  Other archives ( Real Estate discussion Medical topics )