如何将SQL查询转换为Django模型中的代码

时间:2019-03-23 02:35:56

标签: django-models

我想将sql查询转换为Django模式下的代码 我的模特

class Plan(BaseModel):
    id = models.UUIDField(auto_created=True, default=uuid.uuid4, unique=True, primary_key=True)
    status = models.CharField(max_length=45, default=AppConstants.StatusPlan.Draft)
    name = models.CharField(max_length=45, blank=True)

    total_price = models.DecimalField(max_digits=20, decimal_places=2, default=0)
    # Foreign Key
    service_items = models.ManyToManyField('service_items.ServicesItems',
                                           through='plan_services.PlanService',
                                           related_name='plan_service_items')
    participant = models.ForeignKey('participants.Participant', related_name='plan', on_delete=models.CASCADE)

我的SQL查询:

SELECT
    *
FROM
    plans_plan
WHERE
    (participant_id, created_at) IN (
        SELECT
            participant_id,
            max(created_at)
        FROM
            plans_plan
        GROUP BY
            participant_id

请帮助我! 谢谢所有

0 个答案:

没有答案