2011-04-10 33 views
0

我正在將Twitter克隆作爲類項目。我有一個配置文件模型,我想讓他們兩個之間的關係。我做了以下和它的工作確定:在Django中避免與自己的關係

class Profile(models.Model): 
    ... 
    followers = models.ManyToManyField('self', symmetrical=False, related_name='following') 

現在,我該怎麼做才能避免與自身有關(用戶自己之後)個人資料?

我嘗試使用乾淨,但它不工作。

def clean_followers(self): 
     self.followers.remove(self) 

回答

0

我認爲什麼是最適合你的項目是要保持它,因爲它是變本加厲,我會強制每個用戶都跟隨自己,因爲當你顯示主網頁的鳴叫,you'll希望用戶看到自己的。如果你設法完成你想要的任務,你必須做2分隔查詢(或至少更醜陋的一個)來獲取所有推文。

您可以使用非常簡單的模板標記清理「追隨者列表」。

{% for follower_profile in current_profile.followers %} 
    {% ifnotequal current_profile follower_profile %} 
     <li>{{ user.username }}</li> 
    {% endifnotequal %} 
{% endfor %}