ドキュメントの1つの例を除けば、djangoが親オブジェクトから子オブジェクトにアクセスできる名前をどのように正確に選択するかについてのドキュメントは見つかりません。彼らの例では、彼らは次のことをします:
class Place(models.Model):
name = models.CharField(max_length=50)
address = models.CharField(max_length=80)
def __unicode__(self):
return u"%s the place" % self.name
class Restaurant(models.Model):
place = models.OneToOneField(Place, primary_key=True)
serves_hot_dogs = models.BooleanField()
serves_pizza = models.BooleanField()
def __unicode__(self):
return u"%s the restaurant" % self.place.name
# Create a couple of Places.
>>> p1 = Place(name='Demon Dogs', address='944 W. Fullerton')
>>> p1.save()
>>> p2 = Place(name='Ace Hardware', address='1013 N. Ashland')
>>> p2.save()
# Create a Restaurant. Pass the ID of the "parent" object as this object's ID.
>>> r = Restaurant(place=p1, serves_hot_dogs=True, serves_pizza=False)
>>> r.save()
# A Restaurant can access its place.
>>> r.place
<Place: Demon Dogs the place>
# A Place can access its restaurant, if available.
>>> p1.restaurant
したがって、彼らの例では、その名前を明示的に定義せずに、単にp1.restaurantを呼び出します。Djangoは、名前が小文字で始まることを前提としています。FancyRestaurantのように、オブジェクト名に複数の単語が含まれている場合はどうなりますか?
補足:この方法でUserオブジェクトを拡張しようとしています。それが問題かもしれませんか?