如何在DRF中左联接模型(或)表

时间:2019-12-17 05:43:20

标签: mysql django-rest-framework

我有两个模型Employee和AccessDr。

Employee Model =>

class Employee(models.Model):
    empid = models.CharField(max_length=20, unique=True)
    empname = models.CharField(max_length=50)
    phone = models.CharField(max_length=20, blank=True)
    token = models.ForeignKey(
        Company, to_field='token', on_delete=models.CASCADE)

    def __str__(self):
        return "%s" % (self.empid)

    class Meta:
        managed = True
        db_table = 'employee'

AccessDr模型=>

class AccessDr(models.Model):
    empid = models.ForeignKey(
        Employee, to_field='empid', on_delete=models.CASCADE)
    _date = models.DateField()
    _time = models.IntegerField()
    device = models.CharField(max_length=5)
    takey = models.CharField(max_length=3, default='00')
    token = models.ForeignKey(
        Company, to_field='token', on_delete=models.CASCADE)

    def __str__(self):
        return "%s %s" % (self.empid, self._date)

    class Meta:
        ordering = ['_date']
        managed = True
        db_table = 'tstrdoor'

当对AccessDr的请求(如SQL)左联接时,我想返回对象,示例json =>

{
empid:'',
empname:'',  <=this one from employee model
phone:'',  <=this one from employee model
_date:'',
_time:'',
.
.


}

我该如何做到这一点?

1 个答案:

答案 0 :(得分:0)

看看这个LEFT JOIN Django ORM

也请阅读https://docs.djangoproject.com/en/2.2/topics/db/queries/#spanning-multi-valued-relationships

您可以打印查询,以查看查询如何通过

转换为SQL。
Employes=AccessDr.objects.values('employee__empname','employee__phone')
print(Employes.query)

您是否在视图中使用序列化器?在这种情况下,您可以创建一个序列化器字段 它将协定数据作为这样的数组放置

class CustomerSerializer(serializers.ModelSerializer):
agreements = AgreementSerializer(many=True, read_only=True)


class Meta:
    model = Customer
    fields = [
        'id',
        'username',
        'mail',
        'fName',
        'lName',
        'fNameEng',
        'lNameEng',
        'personalId',
        'phone',
        'crmId',  # "ID": "20995",
        'agreements',
    ]
    depth = 1

您是否在视图中使用序列化器?在这种情况下,您可以创建序列化器字段 它将协定数据作为这样的数组

{
  "id": 985,
  "username": null,
  "mail": "undefined",
  "fName": "Merab",
  "lName": "Dasv",
  "fNameEng": "Merab",
  "lNameEng": "Dasv",
  "personalId": "01022342346003629",
  "phone": "5912324234282331",
  "crmId": 1439,
  "agreements": [
    {
      "id": 884,
      "signDate": "2015-04-16",
      "accountDate": "2015-05-01",
      "amount": 0,
      "comBalance": -1445.0,
      "status": 1,
      "details": [
        {
          "square": 32.38,
          "amount": 35.0,
          "object": {
            "id": 578,
            "object": 2,
            "block": 1,
            "floor": 19,
            "flat": "7",
            "cadastre": "05.24.04.055.01.563",
            "square": 32.38,
            "pCounter": 25915123146,
            "wCounter": 104412312435,
            "accountDate": "2015-04-01T00:00:00",
            "comBalance": -1445.0,
            "comDeptAmount": 1895.0,
            "rentDate": null,
            "active": 1,
            "finaAmount": 0,
            "crmId": 0
          }
        },

      ]
    },

  ]
}