Hello Craig!
Thanks for the reply!
Actually I want to use a form that will save the Employee data and
would be sent to the Employee class, and this class has relationship
with the Person and Address, the data would automatically be
persisted.
Follow the instructions you suggested and they match what the manual
says:
http://docs.djangoproject.com/en/dev/topics/db/models/#relationships

I did classes like this:

class Employee (models.Model):
    person = models.OneToOneField(Person) # only one person related
    address = models.OneToOneField(Address) # only one address for
employee
    sectors= models.ForeignKey(Sectors) # one or various sectors for
the employee

This is the SQL:

CREATE TABLE "employee_employee" (
    "id" serial NOT NULL PRIMARY KEY,
    "person_id" integer NOT NULL UNIQUE,
    "address_id" integer NOT NULL UNIQUE,
    "sectors_id" integer NOT NULL
)

Thanks for help!




On 17 abr, 17:21, W Craig Trader <[email protected]> wrote:
> On 04/16/2011 04:35 PM, Guevara wrote:
>
>
>
>
>
>
>
>
>
> > Hello!
> > I have two class, Person and employee, i need make a composition for
> > this (Prefer composition over inheritance):
>
> > class Person(models.Model):
> >      name = models.CharField(max_length=50)
> >      date_inclusion = models.DateField()
> >      # others fields
>
> > class Employee(models.Model):
> >      person = models.OneToOneField(Person, primary_key=True)
> >      address = models.OneToOneField(Address, primary_key=True)
>
> > The SQL generate is:
>
> > BEGIN;
> > CREATE TABLE "person_person" (
> >      "id" serial NOT NULL PRIMARY KEY,
> >      "name" varchar(50) NOT NULL,
> >      "date_inclusion" date NOT NULL,
> > )
> > ;
> > CREATE TABLE "employee_employee" (
> >      "person_id" integer NOT NULL PRIMARY KEY,
> >      "address_id" integer NOT NULL PRIMARY KEY,
> > )
> > ;
>
> > This is correct? Should generate the id of the employee?
> > Proxy models could be used for this case?
>
> > Thanks!
>
> It's correct in that Django has done exactly what you've told it you want, 
> but I doubt that what
> you've told it is what you REALLY want.
>
> If your goal is to have an employee object that has direct access to all of 
> its related person
> object, and whose only new data field is a reference to an address object, 
> then you should change
> the Employee model to this:
>
> class Address(models.Model):
>      pass
>
> class Person(models.Model):
>      name = models.CharField(max_length=50)
>      date_inclusion = models.DateField()
>
> class Employee(models.Model):
>      person = models.OneToOneField(Person)
>      address = models.ForeignKey(Address)
>
> This will change the generated employee table to something like this (for 
> SQLite3):
>
> CREATE TABLE "foo_address" (
>      "id" integer NOT NULL PRIMARY KEY
> )
> ;
> CREATE TABLE "foo_person" (
>      "id" integer NOT NULL PRIMARY KEY,
>      "name" varchar(50) NOT NULL,
>      "date_inclusion" date NOT NULL
> )
> ;
> CREATE TABLE "foo_employee" (
>      "id" integer NOT NULL PRIMARY KEY,
>      "person_id" integer NOT NULL UNIQUE REFERENCES "foo_person" ("id"),
>      "address_id" integer NOT NULL REFERENCES "foo_address" ("id")
> )
> ;
> CREATE INDEX "foo_employee_b213c1e9" ON "foo_employee" ("address_id");
>
> With these models, every Employee object will have an equivalent Person 
> object (though you may have Persons without corresponding Employees).  You 
> can then use these models as follows:
>
> (InteractiveConsole)>>>  from foo.models import *
> >>>  from datetime import datetime
> >>>  now = datetime.now()
> >>>  p = Person( name='Tom', date_inclusion=now )
> >>>  p.save()
> >>>  a = Address()
> >>>  a.save()
> >>>  e = Employee( person=p, address=a )
> >>>  e.save()
> >>>  e.person.name
>
> 'Tom'
>
> - Craig -

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected].
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.

Reply via email to