Re: How to define a "has-a" relationship in dJango models

2009-09-17 Thread PlanetUnknown

Thanks All.

Thanks Javier.
That makes sense, I'm implementing it via a OneToOe relationship now.



On Sep 17, 4:38 am, Tom Evans  wrote:
> On Wed, 2009-09-16 at 17:15 -0500, Javier Guerra wrote:
> > On Wed, Sep 16, 2009 at 1:01 PM, PlanetUnknown
> >  wrote:
>
> > > Thanks Karen.
> > > Let me explain it a bit more.
> > > e.g.
> > > All CONTACT details are present in one table - email, home address,
> > > work address, home phone, work phone etc.
> > > Forget about the statement about growing for now.
> > > Since each user "has-a" contact it is a pure one-to-one relationship
> > > and not a one-to-many or many-to-one; each user in the USER table will
> > > have only one corresponding entry in the CONTACT table.
> > > Does this help explaining the issue ?
> > > The above is just an example, the main question is how does one
> > > usually implement a "has-a" relationship in dJango.
>
> > i think a big part of your problem is that you're using java-inspired
> > OOP terminology.  it's much easier if you use RDBMS terminology (after
> > all, it will all be stored in a RDBMS).  I guess that the Oracle/Java
> > systems you're used to show the DB as a persistence system for
> > objects, while Django's ORM creates classes that represent the DB.
> > same thing, different philosophies.
>
> > the many-to-one relationship is exactly what you said you used in
> > Oracle: a user-id field on the Contact table.
>
> To me that indicates the wrong relationship. The OP said that all Users
> have exactly one Contact. He didn't say that Contacts have users. He
> could have a Business model, which could have many contacts etc.
>
> I would implement this as ForeignKey to Contact on the User model, with
> a unique constraint.
>
>
>
> > the one-to-one relationship is the same thing, but with an added
> > UNIQUE constraint on that key, so that you can have only one contact
> > for each user.  it has nothing to do with inheritance.  the fact that
> > inheritance is implemented with one-to-one relationships is just
> > incidental.
>
> > in most cases, you want the possibility of several contacts per user,
> > so you can use the many-to-one.  the only drawback if that you get an
> > extra step to go from user to contact: "User.contact_set.all()". if
> > you use one-to-one, the ORM lets you skip that step and write simply
> > "User.contact".
>
>
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to define a "has-a" relationship in dJango models

2009-09-17 Thread Adam N

> >  wrote:
>
> > > Thanks Karen.
> > > Let me explain it a bit more.
> > > e.g.
> > > All CONTACT details are present in one table - email, home address,
> > > work address, home phone, work phone etc.
> > > Forget about the statement about growing for now.
> > > Since each user "has-a" contact it is a pure one-to-one relationship
> > > and not a one-to-many or many-to-one; each user in the USER table will
> > > have only one corresponding entry in the CONTACT table.
> > > Does this help explaining the issue ?
> > > The above is just an example, the main question is how does one
> > > usually implement a "has-a" relationship in dJango.
>

There are a few things being overlooked here:

1. This cookie has been baked before.  Instead of talking about it in
the abstract, check out django-profiles or django-basic-apps (and
there are more I'm sure).  That app will give you much better control
over contact details for a user in a robust way.  Just use that app
and you can focus on import things like what your project does:

http://bitbucket.org/ubernostrum/django-profiles/wiki/Home
http://code.google.com/p/django-basic-apps/source/browse/#svn/trunk/profiles

2.  There are two ways to handle the issue generally - A symmetrical
one-to-one key allowing any profile to get its user or vice versa, and
extending the User class with something like:

class Profile(User):
email = models
phone = models

@PlanetUnkown - you haven't really stated what's wrong with extending
the user like that, nor what is wrong with the one-to-one, so nobody
can help you any more than to tell you either one of those will do
what you want it to do.  I personally would extend the User model for
your limited need but most profile apps use a ForeignKey field
(OneToMany) so as to support multiple profiles per user.

Cheers,
Adam
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to define a "has-a" relationship in dJango models

2009-09-17 Thread Tom Evans

On Wed, 2009-09-16 at 17:15 -0500, Javier Guerra wrote:
> On Wed, Sep 16, 2009 at 1:01 PM, PlanetUnknown
>  wrote:
> >
> > Thanks Karen.
> > Let me explain it a bit more.
> > e.g.
> > All CONTACT details are present in one table - email, home address,
> > work address, home phone, work phone etc.
> > Forget about the statement about growing for now.
> > Since each user "has-a" contact it is a pure one-to-one relationship
> > and not a one-to-many or many-to-one; each user in the USER table will
> > have only one corresponding entry in the CONTACT table.
> > Does this help explaining the issue ?
> > The above is just an example, the main question is how does one
> > usually implement a "has-a" relationship in dJango.
> 
> 
> i think a big part of your problem is that you're using java-inspired
> OOP terminology.  it's much easier if you use RDBMS terminology (after
> all, it will all be stored in a RDBMS).  I guess that the Oracle/Java
> systems you're used to show the DB as a persistence system for
> objects, while Django's ORM creates classes that represent the DB.
> same thing, different philosophies.
> 
> the many-to-one relationship is exactly what you said you used in
> Oracle: a user-id field on the Contact table.

To me that indicates the wrong relationship. The OP said that all Users
have exactly one Contact. He didn't say that Contacts have users. He
could have a Business model, which could have many contacts etc.

I would implement this as ForeignKey to Contact on the User model, with
a unique constraint.

> 
> the one-to-one relationship is the same thing, but with an added
> UNIQUE constraint on that key, so that you can have only one contact
> for each user.  it has nothing to do with inheritance.  the fact that
> inheritance is implemented with one-to-one relationships is just
> incidental.
> 
> in most cases, you want the possibility of several contacts per user,
> so you can use the many-to-one.  the only drawback if that you get an
> extra step to go from user to contact: "User.contact_set.all()". if
> you use one-to-one, the ORM lets you skip that step and write simply
> "User.contact".
> 


--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to define a "has-a" relationship in dJango models

2009-09-16 Thread Craig McClanahan

On Wed, Sep 16, 2009 at 3:15 PM, Javier Guerra  wrote:
>
> On Wed, Sep 16, 2009 at 1:01 PM, PlanetUnknown
>  wrote:
>>
>> Thanks Karen.
>> Let me explain it a bit more.
>> e.g.
>> All CONTACT details are present in one table - email, home address,
>> work address, home phone, work phone etc.
>> Forget about the statement about growing for now.
>> Since each user "has-a" contact it is a pure one-to-one relationship
>> and not a one-to-many or many-to-one; each user in the USER table will
>> have only one corresponding entry in the CONTACT table.
>> Does this help explaining the issue ?
>> The above is just an example, the main question is how does one
>> usually implement a "has-a" relationship in dJango.
>
>
> i think a big part of your problem is that you're using java-inspired
> OOP terminology.  it's much easier if you use RDBMS terminology (after
> all, it will all be stored in a RDBMS).  I guess that the Oracle/Java
> systems you're used to show the DB as a persistence system for
> objects, while Django's ORM creates classes that represent the DB.
> same thing, different philosophies.

I'm not much into "my XXX is bigger/stronger/better than your XXX"
games, but it is worth noting that Java based ORM frameworks
(including JPA and Hibernate) definitely understand what a one-to-one
versus a many-to-one relationship means :-).

Craig McClanahan

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to define a "has-a" relationship in dJango models

2009-09-16 Thread Javier Guerra

On Wed, Sep 16, 2009 at 1:01 PM, PlanetUnknown
 wrote:
>
> Thanks Karen.
> Let me explain it a bit more.
> e.g.
> All CONTACT details are present in one table - email, home address,
> work address, home phone, work phone etc.
> Forget about the statement about growing for now.
> Since each user "has-a" contact it is a pure one-to-one relationship
> and not a one-to-many or many-to-one; each user in the USER table will
> have only one corresponding entry in the CONTACT table.
> Does this help explaining the issue ?
> The above is just an example, the main question is how does one
> usually implement a "has-a" relationship in dJango.


i think a big part of your problem is that you're using java-inspired
OOP terminology.  it's much easier if you use RDBMS terminology (after
all, it will all be stored in a RDBMS).  I guess that the Oracle/Java
systems you're used to show the DB as a persistence system for
objects, while Django's ORM creates classes that represent the DB.
same thing, different philosophies.

the many-to-one relationship is exactly what you said you used in
Oracle: a user-id field on the Contact table.

the one-to-one relationship is the same thing, but with an added
UNIQUE constraint on that key, so that you can have only one contact
for each user.  it has nothing to do with inheritance.  the fact that
inheritance is implemented with one-to-one relationships is just
incidental.

in most cases, you want the possibility of several contacts per user,
so you can use the many-to-one.  the only drawback if that you get an
extra step to go from user to contact: "User.contact_set.all()". if
you use one-to-one, the ORM lets you skip that step and write simply
"User.contact".

-- 
Javier

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to define a "has-a" relationship in dJango models

2009-09-16 Thread Karen Tracey
On Wed, Sep 16, 2009 at 2:01 PM, PlanetUnknown wrote:

>
> Thanks Karen.
> Let me explain it a bit more.
> e.g.
> All CONTACT details are present in one table - email, home address,
> work address, home phone, work phone etc.
> Forget about the statement about growing for now.
> Since each user "has-a" contact it is a pure one-to-one relationship
> and not a one-to-many or many-to-one; each user in the USER table will
> have only one corresponding entry in the CONTACT table.
> Does this help explaining the issue ?
> The above is just an example, the main question is how does one
> usually implement a "has-a" relationship in dJango.
>
>
Then you are saying you want a OneToOneField.  Your rejection of that
alternative based on the fact that "seems to be more of an inheritance type"
of relationship doesn't make sense to me.  The fact that it may be used to
implement inheritance relationships doesn't mean it cannot be used for the
non-inheritance case.

Karen

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to define a "has-a" relationship in dJango models

2009-09-16 Thread PlanetUnknown

Thanks Karen.
Let me explain it a bit more.
e.g.
All CONTACT details are present in one table - email, home address,
work address, home phone, work phone etc.
Forget about the statement about growing for now.
Since each user "has-a" contact it is a pure one-to-one relationship
and not a one-to-many or many-to-one; each user in the USER table will
have only one corresponding entry in the CONTACT table.
Does this help explaining the issue ?
The above is just an example, the main question is how does one
usually implement a "has-a" relationship in dJango.

On Sep 16, 10:52 am, Karen Tracey  wrote:
> On Wed, Sep 16, 2009 at 10:15 AM, PlanetUnknown
> wrote:
>
>
>
> > For example - User HAS "Contacts"; User HAS "Preferences"
> > Usually (I'm from an Oracle/Java background) the Contacts table would
> > have a "user-id" foreign key.
> > However Django models refer Foreign Key relations as "Many-to-one",
> > but that is not true in my case.
>
> Why is it not true in your case?  You say 'User HAS "Contacts", thus it
> sounds like Many different Contact instances may be associated with one
> single User.  Many-to-One.  If a user can only have a single Contact then
> you might want OneToOne, but you have said in a follow up message to that
> suggestion that that is not what you want.  So I am having a hard time
> understand what it is, exactly, that you do want.
>
> > There is only one Contact table for a User and it might keep growing
> > as new fields are added.
>
> What do you mean by fields?  Fields in a Django model correspond to columns
> in the database table, and you generally do not want those to be growing
> over time.  If you mean rows in the table, then again, it sounds like you
> want the many-to-one relation provided by ForeignKey.
>
> The "for a User" clause on your first sentence also sounds a bit wrong.
> Normally, there is only one Contact table, period.  Which user is associated
> with a given row in the table would be determined by the value of the User
> column in the table.
>
> Karen
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to define a "has-a" relationship in dJango models

2009-09-16 Thread Karen Tracey
On Wed, Sep 16, 2009 at 10:15 AM, PlanetUnknown
wrote:

>
> For example - User HAS "Contacts"; User HAS "Preferences"
> Usually (I'm from an Oracle/Java background) the Contacts table would
> have a "user-id" foreign key.
> However Django models refer Foreign Key relations as "Many-to-one",
> but that is not true in my case.
>

Why is it not true in your case?  You say 'User HAS "Contacts", thus it
sounds like Many different Contact instances may be associated with one
single User.  Many-to-One.  If a user can only have a single Contact then
you might want OneToOne, but you have said in a follow up message to that
suggestion that that is not what you want.  So I am having a hard time
understand what it is, exactly, that you do want.



> There is only one Contact table for a User and it might keep growing
> as new fields are added.
>

What do you mean by fields?  Fields in a Django model correspond to columns
in the database table, and you generally do not want those to be growing
over time.  If you mean rows in the table, then again, it sounds like you
want the many-to-one relation provided by ForeignKey.

The "for a User" clause on your first sentence also sounds a bit wrong.
Normally, there is only one Contact table, period.  Which user is associated
with a given row in the table would be determined by the value of the User
column in the table.

Karen

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to define a "has-a" relationship in dJango models

2009-09-16 Thread PlanetUnknown

The OneToOne relationship seems to be more of an inheritance type of
relationship, hence that cannot be used.

On Sep 16, 10:28 am, Craig McClanahan  wrote:
> On Wed, Sep 16, 2009 at 7:15 AM, PlanetUnknown
>
>  wrote:
>
> > For example - User HAS "Contacts"; User HAS "Preferences"
> > Usually (I'm from an Oracle/Java background) the Contacts table would
> > have a "user-id" foreign key.
> > However Django models refer Foreign Key relations as "Many-to-one",
> > but that is not true in my case.
> > There is only one Contact table for a User and it might keep growing
> > as new fields are added.
> > Don't want to clutter up the User Profile with all this.
>
> > Thanks for any ideas you can share.
>
> Sounds like you might want a models.OneToOneField() instead of a
> models.ForeignKey field.
>
>  http://www.djangoproject.com/documentation/models/one_to_one/
>
> Craig
>
>
>
>
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to define a "has-a" relationship in dJango models

2009-09-16 Thread Craig McClanahan

On Wed, Sep 16, 2009 at 7:15 AM, PlanetUnknown
 wrote:
>
> For example - User HAS "Contacts"; User HAS "Preferences"
> Usually (I'm from an Oracle/Java background) the Contacts table would
> have a "user-id" foreign key.
> However Django models refer Foreign Key relations as "Many-to-one",
> but that is not true in my case.
> There is only one Contact table for a User and it might keep growing
> as new fields are added.
> Don't want to clutter up the User Profile with all this.
>
> Thanks for any ideas you can share.

Sounds like you might want a models.OneToOneField() instead of a
models.ForeignKey field.

  http://www.djangoproject.com/documentation/models/one_to_one/

Craig


> >
>

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



How to define a "has-a" relationship in dJango models

2009-09-16 Thread PlanetUnknown

For example - User HAS "Contacts"; User HAS "Preferences"
Usually (I'm from an Oracle/Java background) the Contacts table would
have a "user-id" foreign key.
However Django models refer Foreign Key relations as "Many-to-one",
but that is not true in my case.
There is only one Contact table for a User and it might keep growing
as new fields are added.
Don't want to clutter up the User Profile with all this.

Thanks for any ideas you can share.
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---