2015-01-16 29 views
5

Próbuję skonfigurować model Django, który działa jako klasa podstawowa dla innych modeli. Model Base ma dwa pola ForeignKey dla innych obiektów tej samej klasy (TestForeign). Mogę uzyskać modele działające przy użyciu dziedziczenia multitable, ale chcę używać dziedziczenia abstrakcyjnego modelu, ponieważ przeczytałem, że istnieje kilka performance issues when using multitable inheritance.Zderzenia kluczy obcych podczas używania abstrakcyjnego dziedziczenia wielokrotnego w Django

Poniższy przykład działa, gdy używam dziedziczenia wielościetnego (abstract = False), ale kończy się niepowodzeniem, gdy uruchamiam go z dziedziczeniem abstrakcyjnym.

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) 

Kiedy synchronizację bazy danych pojawia się następujący błąd:

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'. 

Czy jest jakiś sposób mogę odniesienia klasy bazowej, aby nie kolidować z modeli pochodnych?

Odpowiedz

8

Użyj %(class)s i %(app_label)s w Twoim powiązanym imieniu, jak określono w the docs.

+1

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

Powiązane problemy