2016-10-15 63 views
0

我想寫一個Django模型,它可以與自己有多對多的關係。創建一個Django模型,可以有自己的多對多關係

這是我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.', 
    ) 

當我運行manage.py makemigrations我收到以下錯誤

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

請告訴我,這是可能的,並解釋如何做到這一點。由於

+0

變化'SimilarApps'到'MySimilarApps'(我的意思是改變型號名稱) – itzMEonTV

+0

只是去嘗試,我也得到同樣的錯誤 – HenryM

回答

0

這已經通過makemigrations剛剛和我要測試它:

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.', 
    )  
相關問題