On 18.06.2009 14:05, Ramiro Morales wrote:
> 
> Why do you thing there should be a automatically created one to one 
> relationship from Customer to Contact named "contact"?.

Why I think there should be a automatic one-to-one relation from
Customer to Contact? Because that's what the MTI documentation says (as
far as I understand it):

> Specifying the parent link field
>
> As mentioned, Django will automatically create a OneToOneField
> linking your child class back any non-abstract parent models. If you
> want to control the name of the attribute linking back to the parent,
> you can create your own OneToOneField and set parent_link=True to
> indicate that your field is the link back to the parent class.

http://docs.djangoproject.com/en/dev/topics/db/models/#specifying-the-parent-link-field

It's true that I didn't notice that the example
(http://www.djangoproject.com/documentation/models/one_to_one/) isn't
about MTI at all (rather a kind of emulation).

Okay, when I add an explicit parent link in my example:

    contact = models.OneToOneField(contacts.Contact,parent_link=True)

then the TypeError disappears. But still I would now expect that the
following works:

  >>> c = Customer(contact=luc)
  >>> c.save()
  >>> c.last_name
  Saffre

But it fails, saying:

  Failed example:
      c.last_name

  Expected:
      Saffre
  Got:
      ''

It looks as if Django doesn't "copy" the values of the existing Contact
into the Customer. The last_name field was inherited (it didn't say
"AttributeError: 'Customer' object has no attribute 'last_name'") but it
is empty.

Note that I'm using Django development version, revision 11066...

I guess that this is a bug and that it has to do with ticket #7623
(http://code.djangoproject.com/ticket/7623).

Thus cc to django-developers and a new very short example showing the
problem. (Should I wait for feedback from developers before opening a
new ticket?)

Luc


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

"""

Small example for the problem described in ticket #7623
(http://code.djangoproject.com/ticket/7623)

With revision 11066, the following doctest fails::

    Failed example:
        r
    Expected:
        <Restaurant: Demon Dogs>
    Got:
        <Restaurant: >


>>> p1 = Place(name='Demon Dogs')
>>> p1.save()

# Create a Restaurant from this Place. 
>>> r = Restaurant(place=p1, serves_hot_dogs=True)
>>> r.save()
>>> r
<Restaurant: Demon Dogs>

"""

from django.db import models

class Place(models.Model):
    name = models.CharField(max_length=50)

    def __unicode__(self):
        return self.name

class Restaurant(Place):
  
    serves_hot_dogs = models.BooleanField()
    
    # the following explicit parent_link should not be necessary according to 
    # http://docs.djangoproject.com/en/dev/topics/db/models/#specifying-the-parent-link-field
    place = models.OneToOneField(Place,parent_link=True)


Reply via email to