sql查询到django查询

m3eecexj  于 2021-07-26  发布在  Java
关注(0)|答案(0)|浏览(214)

我想知道如何将下面的sql查询转换成django查询

select main_project.name, (main_contacts.first_name) as Name, group_concat(main_property.property_id) as Properties, sum(main_property.area), (main_contacts.first_name||main_project.name) as CONPROP 
from main_project
INNER join main_property on main_property.project_id = main_project.id
INNER join main_property_owner ON main_property_owner.property_id = main_property.id
INNER JOIN main_contacts ON main_contacts.id = main_property_owner.contacts_id
group by CONPROP

型号.py

class Contacts(models.Model):
    first_name = models.CharField(max_length=15)
    last_name = models.CharField(max_length=15, null=True, blank=True)
    mobile = models.CharField(max_length=10, unique=True)
    email = models.EmailField(unique=True, null=True, blank=True)

    def __str__(self):
        return f'{self.first_name} - {self.mobile}'

class Project(models.Model):
    name = models.CharField(max_length=100)
    address = models.ForeignKey(Address, on_delete=models.CASCADE)

    def __str__(self):
        return self.name

class Property(models.Model):
    added_on = models.DateTimeField(auto_now_add=True)
    creator = models.ForeignKey(User, null=True, on_delete=models.SET_NULL, related_name='creator',
                                limit_choices_to={'is_staff': True})
    property_id = models.CharField(max_length=10, unique=True)
    project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='properties')
    owner = models.ManyToManyField(Contacts, blank=True)
    area = models.DecimalField(max_digits=20, decimal_places=2)
    internal = models.TextField(null=True, blank=True)
    external = models.TextField(null=True, blank=True)

    def __str__(self):
        return self.property_id

我已经知道,这将是用'注解',但不知何故,我无法做到这一点。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题