2015-01-16 13 views
5

Tôi đang cố gắng thiết lập Mô hình Django hoạt động như một lớp cơ sở cho các mô hình khác. Mô hình cơ sở có hai trường ForeignKey cho các đối tượng khác của cùng một lớp (TestForeign). Tôi có thể nhận được các mô hình làm việc sử dụng thừa kế đa trị nhưng tôi muốn sử dụng kế thừa mô hình trừu tượng vì tôi đã đọc rằng có một số performance issues when using multitable inheritance.ForeignKeys xung đột khi sử dụng đa thừa kế trừu tượng trong Django

Ví dụ sau đây hoạt động khi tôi sử dụng đa thừa kế (abstract = False) nhưng không thành công khi tôi chạy nó với thừa kế trừu tượng.

class TestForeign(models.Model): 
    test = models.CharField(max_length=100) 

class BaseModel(models.Model): 
    # specified related_name to avoid reverse accesor clash 
    foo = models.ForeignKey(TestForeign, related_name='fooooo') 
    bar = models.ForeignKey(TestForeign) 

    class Meta: 
    abstract = True 

class AnotherModel(BaseModel): 
    bla = models.CharField(max_length=100) 

class YetAnotherModel(BaseModel): 
    bla = models.CharField(max_length=100) 

Khi tôi đồng bộ hóa cơ sở dữ liệu tôi nhận được lỗi sau:

ERRORS: 
Base.AnotherModel.bar: (fields.E304) Reverse accessor for 'AnotherModel.bar' clashes with reverse accessor for 'YetAnotherModel.bar'. 
HINT: Add or change a related_name argument to the definition for 'AnotherModel.bar' or 'YetAnotherModel.bar'. 
Base.AnotherModel.bar: (fields.E305) Reverse query name for 'AnotherModel.bar' clashes with reverse query name for 'YetAnotherModel.bar'. 
HINT: Add or change a related_name argument to the definition for 'AnotherModel.bar' or 'YetAnotherModel.bar'. 
Base.AnotherModel.foo: (fields.E304) Reverse accessor for 'AnotherModel.foo' clashes with reverse accessor for 'YetAnotherModel.foo'. 
HINT: Add or change a related_name argument to the definition for 'AnotherModel.foo' or 'YetAnotherModel.foo'. 
Base.AnotherModel.foo: (fields.E305) Reverse query name for 'AnotherModel.foo' clashes with reverse query name for 'YetAnotherModel.foo'. 
HINT: Add or change a related_name argument to the definition for 'AnotherModel.foo' or 'YetAnotherModel.foo'. 
Base.YetAnotherModel.bar: (fields.E304) Reverse accessor for 'YetAnotherModel.bar' clashes with reverse accessor for 'AnotherModel.bar'. 
HINT: Add or change a related_name argument to the definition for 'YetAnotherModel.bar' or 'AnotherModel.bar'. 
Base.YetAnotherModel.bar: (fields.E305) Reverse query name for 'YetAnotherModel.bar' clashes with reverse query name for 'AnotherModel.bar'. 
HINT: Add or change a related_name argument to the definition for 'YetAnotherModel.bar' or 'AnotherModel.bar'. 
Base.YetAnotherModel.foo: (fields.E304) Reverse accessor for 'YetAnotherModel.foo' clashes with reverse accessor for 'AnotherModel.foo'. 
HINT: Add or change a related_name argument to the definition for 'YetAnotherModel.foo' or 'AnotherModel.foo'. 
Base.YetAnotherModel.foo: (fields.E305) Reverse query name for 'YetAnotherModel.foo' clashes with reverse query name for 'AnotherModel.foo'. 
HINT: Add or change a related_name argument to the definition for 'YetAnotherModel.foo' or 'AnotherModel.foo'. 

Có cách nào tôi có thể nhận được các tài liệu tham khảo của lớp cơ sở không để xung đột trong các mô hình có nguồn gốc?

Trả lời

8

Sử dụng %(class)s%(app_label)s trong tên liên quan của bạn, như được chỉ định trong the docs.

+1

E.G: foo = models.ForeignKey (TestForeign, related_name = "% (app_label) s _% (lớp) s_foo") – ninapavlich

Các vấn đề liên quan