多表Django连接



对Django来说比较新。我正在Django中构建一个轻型CRM。我建立的MYSQL数据库使用关联实体来帮助处理多对多关系。

我正在尝试使用关联实体"contactdeal"连接"交易";与"contacts".

见下面的模型和视图。

Models:

class Account1(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, blank=True, null=True)       
    address = models.CharField(max_length=255, blank=True, null=True)    
    city = models.CharField(max_length=255, blank=True, null=True)       
    state = models.CharField(max_length=255, blank=True, null=True)      
    country = models.CharField(max_length=255, blank=True, null=True)    
    description = models.CharField(max_length=255, blank=True, null=True)
    phone = models.CharField(max_length=255, blank=True, null=True)      
    email = models.CharField(max_length=255, blank=True, null=True)
    biztype = models.CharField(max_length=255, blank=True, null=True)
    notes = models.CharField(max_length=255, blank=True, null=True)
    class Meta:
        managed = False
        db_table = 'account'
class Contact1(models.Model):
    contact_id = models.AutoField(primary_key=True)
    first_name = models.CharField(max_length=255, blank=True, null=True)
    last_name = models.CharField(max_length=255, blank=True, null=True)
    account_id = models.ForeignKey(Account1, models.DO_NOTHING, db_column='account_id', blank=False, null=False)
    contact_notes = models.TextField(blank=True, null=True)
    contact_email = models.CharField(max_length=255, blank=True, null=True)
    contact_phone = models.CharField(max_length=255, blank=True, null=True)
    contact_status = models.CharField(max_length=255, blank=True, null=True)
    
class Meta:
        managed = False
        db_table = 'contact'
class Contactdeal(models.Model):
    contactdeal_id = models.AutoField(primary_key=True)
    contactdeal_deal_fk = models.ForeignKey('Deal', models.DO_NOTHING, db_column='contactdeal_deal_fk', blank=True, null=True)
    contactdeal_contact_fk = models.ForeignKey(Contact1, models.DO_NOTHING, related_name='contactdeal_contact_fk', db_column='contactdeal_contact_fk', blank=True, null=True)
    class Meta:
        managed = False
        db_table = 'contactdeal'
class Deal(models.Model):
    deal_id = models.AutoField(primary_key=True)
    deal_name = models.CharField(max_length=255, blank=True, null=True)
    est_value = models.DecimalField(max_digits=13, decimal_places=2, blank=True, null=True)
    deal_notes = models.TextField(blank=True, null=True)
    class Meta:
        managed = False
        db_table = 'deal'

View:

def deal(request):
    deal = Contactdeal.objects.select_related('contactdeal_contact_fk', 'contactdeal_deal_fk')
    template = loader.get_template('deal.html')
    context = {
        'deal' : deal,
    }
    return HttpResponse(template.render(context, request))

Template:

{% for x in deal %}
<tbody>
<tr class="table-active">
    <td>{{x.contactdeal_deal_fk.deal_name}}</td>
    <td>{{x.contactdeal_deal_fk.est_value}}</td>
    <td>{{x.contactdeal_contact_fk.first_name}}</td>
    <td>{{x.contactdeal_contact_fk.account_id}}</td>
    <td><a href="updateaccount/{{ x.id }}">Update</a></td>
    <td><a href="deleteaccount/{{x.id}}">Delete</a></td>
</tr>
</tbody>
{% endfor %}

在大多数情况下,我已经成功地使用select_related方法使用关联实体contactdeal来连接联系人和交易。

但是,当我想要显示"account"时,就不足了。每个"contact"属于。我可以显示&;account_id&;也就是连接"触点"的FK表与"帐"表,但我似乎不能想出一种方法来添加另一个连接,将允许我使用"account_id"在联系人表中提取与该"account_id"相关联的名称。在"账户"中表。

我想我明白了。我将尝试在MYSQL中创建一个视图,以实现我希望建立的连接,并将其转化为自己的模型,而不是使用Django管理。

最新更新