2
我有幾個類與ManyToManyField鏈接的模型:Django的多對多對象的字段返回空
class UserProfile(models.Model):
contacts = models.ManyToManyField(Contact)
class Contact(models.Model):
first_name = models.CharField(max_length=50)
在視圖中我傳遞:
contacts_list = request.user_profile.contacts
這將產生數的一些奇怪的字符串
{% for c in contacts_list %}
{{ c }}
{% endfor %}
這會產生什麼:每個模板刷新時間變化
{% for c in contacts_list %}
{{ c.first_name }}
{% endfor %}
在我的聯繫人級我也有定義爲返回first_name
的__unicode__(self)
,那麼,爲什麼ManyToManyField
對象未返回此值?我也無法弄清楚如何成功顯示first_name
字段值。感謝您的任何建議或幫助!
謝謝!我以爲我試過了我花幾個小時查找的所有解決方案,包括這些,但我又試了一次(模板中的解決方案)並重置了整個數據庫,最後它工作。 – Daya