2013-09-26 5 views
3

나는 장고와 책 파이썬 웹 개발을 읽고 그리고 난 책에서이 예를 발견 :장고 모델 상속, 재정 분야

class Book(models.Model): 
      title = models.CharField(max_length=100) 
      genre = models.CharField(max_length=100) 
      num_pages = models.IntegerField() 
      authors = models.ManyToManyField(Author) 

      def __unicode__(self): 
       return self.title 

    class SmithBook(Book): 
     authors = models.ManyToManyField(Author, limit_choices_to={'name__endswith': 'Smith'}) 

그것은 그것 같이 보인다는 작동하지 않는 :

FieldError: Local field 'authors' in class 'SmithBook' clashes with field of similar name from base class 'Book'

나는 장고 1.5.3을 사용하고 있으며 장고 1.0 용이다.

왜 장고에서 상속 할 때 필드를 재정의 할 수 없습니까? Django 1.0에서 가능 했습니까? 아니면 책의 오류입니까?

답변

10

django에서는 1.0이 아니라고 생각하지 마십시오.

In normal Python class inheritance, it is permissible for a child class to override any attribute from the parent class. In Django, this is not permitted for attributes that are Field instances (at least, not at the moment). If a base class has a field called author, you cannot create another model field called author in any class that inherits from that base class.

Field name “hiding” is not permitted - django 1.0에서이 여전히 장고의 최신 버전을 보유하고 있습니다.