2016-10-15 3 views
0

Ich versuche ein Django-Modell zu schreiben, das eine Viele-zu-Viele-Beziehung mit sich selbst haben kann.Erstellen eines Django-Modells, das eine Viele-zu-Viele-Beziehung mit sich selbst haben kann

Das ist mein models.py:

class Apps(models.Model): 
    name = models.CharField(
     verbose_name = 'App Name', 
     max_length = 30 
    ) 
    logo = models.ImageField(
     verbose_name = 'Logo' 
     ) 
    description = models.TextField(
     verbose_name = 'Description' 
    ) 
    google_url = models.URLField(
     verbose_name = 'Google Play Link', 
     null = True 
    ) 
    apple_url = models.URLField(
     verbose_name = 'Apple AppStore Link', 
     null = True 
    ) 
    youtube_url = models.URLField(
     verbose_name = 'Youtube Video Page', 
     null = True 
    ) 
    similar_apps = models.ManyToManyField(
     'self', 
     through   = 'SimilarApps', 
     related_name = 'similar_apps', 
     verbose_name = 'Similar Apps', 
     help_text  = 'Similar Apps', 
     symmetrical  = False 
     ) 

    def __unicode__(self): 
     return u'%s' % (self.user.name) 

class SimilarApps(models.Model): 
    primary = models.ForeignKey(
     Apps, 
     verbose_name = 'Primary App', 
     related_name = 'primary_app', 
     help_text  = 'First of 2 similar Apps.', 
    ) 
    secondary = models.ForeignKey(
     Apps, 
     verbose_name = 'Matched App', 
     related_name = 'matched_app', 
     help_text  = 'Second of 2 similar Apps.', 
    ) 

Als ich manage.py makemigrations betreibe ich die folgende Fehlermeldung erhalten

<class 'appsrfun.admin.SimilarAppsInline'>: (admin.E202) 'appsrfun.SimilarApps' has more than one ForeignKey to 'appsrfun.Apps'. 
appsrfun.Apps.similar_apps: (fields.E302) Reverse accessor for 'Apps.similar_apps' clashes with field name 'Apps.similar_apps'. 
    HINT: Rename field 'Apps.similar_apps', or add/change a related_name argument to the definition for field 'Apps.similar_apps'. 
appsrfun.Apps.similar_apps: (fields.E303) Reverse query name for 'Apps.similar_apps' clashes with field name 'Apps.similar_apps'. 
    HINT: Rename field 'Apps.similar_apps', or add/change a related_name argument to the definition for field 'Apps.similar_apps'. 

Bitte sagen Sie mir dies möglich ist und erklären, wie es zu tun. Dank

+0

Änderung 'SimilarApps' zu' MySimilarApps' (i mittlere Veränderung Modellname) – itzMEonTV

+0

habe gerade versucht, dass und ich bekomme die gleiche Error – HenryM

Antwort

0

Das hat jetzt noch durch makemigrations und ich werde es testen:

class App(models.Model): 
    name = models.CharField(
     verbose_name = 'App Name', 
     max_length  = 30 
    ) 
    logo = models.ImageField(
     verbose_name = 'Logo' 
     ) 
    description = models.TextField(
     verbose_name = 'Description' 
    ) 
    google_url = models.URLField(
     verbose_name = 'Google Play Link', 
     null   = True 
    ) 
    apple_url = models.URLField(
     verbose_name = 'Apple AppStore Link', 
     null   = True 
    ) 
    youtube_url = models.URLField(
     verbose_name = 'Youtube Video Page', 
     null   = True 
    ) 
    similar_app = models.ManyToManyField(
     'self', 
     through   = 'MySimilarApp', 
     verbose_name = 'Similar App', 
     help_text  = 'Similar App', 
     symmetrical  = False, 
     through_fields = ('primary','secondary'), 
     ) 

    def __unicode__(self): 
     return u'%s' % (self.user.name) 

class MySimilarApp(models.Model): 
    primary = models.ForeignKey(
     App, 
     verbose_name = 'Primary App', 
     related_name = 'primary_app', 
     help_text  = 'First of 2 similar Apps.', 
    ) 
    secondary = models.ForeignKey(
     App, 
     verbose_name = 'Matched App', 
     related_name = 'matched_app', 
     help_text  = 'Second of 2 similar Apps.', 
    )  
Verwandte Themen