2012-08-30 71 views
0

我試圖建立一個關係,這將允許多個實體定期更新項目。更確切地說,我想定義模型中使用的術語。Django中間模型

鉛=>這將是項目。

公司=>承包商可以對該項目進行投標。在對項目進行投標/談判時,他們必須定期更新領先地位。

潛在客戶更新=>用於保留更新的表。

但是,當我嘗試運行我得到這個錯誤

"companies.leadupdate: Reverse query name for field 'lead' clashes with m2m field 'Lead.leadupdate'. Add a related_name argument to the definition for 'lead'" 

我決定基於Django的網站https://docs.djangoproject.com/en/1.4/topics/db/models/的例子是類似於即時試圖achieve.I可能是這條線路的服務器完全脫落以及:)

from django.db import models 
class Company(models.Model): 
    comp_name = models.CharField(max_length=200) 
    comp_address = models.CharField(max_length=200) 
    comp_email = models.CharField(max_length=200) 
    comp_zip = models.IntegerField() 
    comp_phone = models.IntegerField() 
    comp_city = models.CharField(max_length=40) 
    def __unicode__(self): 
     return self.comp_name 



class Leadsource(models.Model): 
    assetname = models.CharField(max_length=100) 
    def __unicode__(self): 
     return self.assetname 


class Lead(models.Model): 

    numberofstations = models.IntegerField() 
    comment = models.CharField(max_length=500) 
    #company = models.ManyToManyField(Company) 
    leadsource = models.ForeignKey(Leadsource) 
    submissiondate = models.DateField('Submission Date') 
    cname = models.CharField('Business Name', max_length=100) 
    crep = models.CharField('Principle Contact', max_length=100) 
    cemail = models.CharField(max_length=100) 
    caddress = models.CharField(max_length=100) 
    cnumber = models.CharField(max_length=100) 
    leadupdate = models.ManyToManyField(Company,through='Leadupdate') 

    def __unicode__(self): 
     return u"%s %s %s" % (self.cname, self.crep, self.leadsource) 


class Leadupdate(models.Model): 
    updatedate = models.DateField() 
    update = models.CharField(max_length=400) 
    updatefrom = models.ForeignKey(Company) 
    lead = models.ForeignKey(Lead) 
    def __unicode__(self): 
     return u" %s - %s " % (self.update, self.updatedate) 
+1

那麼問題是什麼? –

回答

0

像錯誤說嘗試related_name參數添加到鉛場的定義,如:

lead = models.ForeignKey(Lead, related_name="lead_related") 

這應該解決問題。

+1

謝謝你做到了! – shaytac