2015-01-16 9 views
5

Sto provando a configurare un modello Django che funzioni come classe base per altri modelli. Il modello Base ha due campi ForeignKey per altri oggetti della stessa classe (TestForeign). Posso far funzionare i modelli usando l'ereditarietà multipla ma voglio usare l'ereditarietà del modello astratto perché ho letto che ci sono alcuni performance issues when using multitable inheritance.ForeignKey che si scontrano quando si utilizza l'ereditarietà multipla astratta in Django

L'esempio seguente funziona quando utilizzo l'ereditarietà multipla (abstract = False) ma non riesco a eseguirlo con l'ereditarietà astratta.

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) 

Quando ho sincronizzare il database ottengo il seguente errore:

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'è un modo posso ottenere i riferimenti della classe base non a scontrarsi nei modelli derivati?

risposta

8

Utilizzare %(class)s e %(app_label)s nel nome correlato, come specificato in the docs.

+1

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

Problemi correlati