我对Django比较陌生。我正在Django中构建一个轻量级的CRM。我构建的MYSQL数据库使用关联实体来帮助处理多对多的关系。
我尝试使用关联实体“contactdeal”来连接“deal”和“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来连接联系人和交易。
但是,当我想显示每个“contact”所属的“account”时,这福尔斯不够了。我可以显示“account_id,“它是链接“contact”表但我似乎无法找到添加另一个连接的方法,该连接将允许我使用contact表中的“account_id”FK从“帐户”表。
1条答案
按热度按时间sauutmhj1#
我想我明白了。我将尝试在MYSQL中创建一个视图,实现我希望建立的连接,并将其转化为自己的模型,而不是使用Django来管理。