0

In my project i have an SQL query for extract some results from two tables like thisone:

SELECT
    read_date, unit_id, device_id, proj_code, var_val
FROM
    public.api_site_varsresults AS SV, public.api_site_results AS SR
WHERE 
    (read_date >= '2021-06-21' AND read_date <= '2021-06-24') AND SV.id_res_id = SR.id

here my models:

class Results(models.Model):
    id = models.AutoField(primary_key=True)
    device = models.ForeignKey(Device, null=True, on_delete=models.SET_NULL)
    proj_code = models.CharField(max_length=400)
    res_key = models.SlugField(max_length=80, verbose_name="Message unique key", unique=True)
    read_date = models.DateTimeField(verbose_name="Datetime of vals readings")
    unit = models.ForeignKey(ModbusDevice, null=True, on_delete=models.SET_NULL)

    def __str__(self):
        return self.device

    class Meta:
        indexes = [
            models.Index(fields=['device', 'unit', 'proj_code']),
        ]


class VarsResults(models.Model):
    id = models.AutoField(primary_key=True)
    id_res = models.ForeignKey(Results, related_name="mainres", on_delete=models.CASCADE)
    var_id = models.ForeignKey(ModbusVariable, null=True, on_delete=models.SET_NULL)
    var_val = models.CharField(max_length=400, blank=True)
    var_hash = models.CharField(max_length=400)

    def __str__(self):
        return self.var_hash

    class Meta:
        indexes = [
            models.Index(fields=['id_res', 'var_id']),
        ]

i wolud to use this query in my django project using ORM but i try to do :

varsresults.objects.filter(<conditions>).fields(<fields>)

but is not correct, i don't know how i can link two tables, select specific fields and filter for that condition in django ORM

Can someone help me please? So many thanks in advance

Manuel

2
  • Show us your models. Commented Jun 24, 2021 at 8:35
  • ok i do it. thanks Commented Jun 24, 2021 at 8:39

1 Answer 1

2

Your Django query would be:

start_date = datetime.date(2021, 06, 21)
end_date = datetime.date(2021, 06, 24)
var_results = VarsResults.objects.filter(
    id_res__read_date__range=(start_date, end_date)
).select_related(
    "id_res"
).values(
    "id_res__read_date",
    "id_res__unit_id",
    "id_res__device_id",
    "id_res__proj_code",
    "var_val",
)

Or to access the fields without getting the values during the query:

start_date = datetime.date(2021, 06, 21)
end_date = datetime.date(2021, 06, 24)
var_results = VarsResults.objects.filter(
    id_res__read_date__range=(start_date, end_date)
).select_related(
    "id_res"
)

read_date = var_results.id_res.read_date
unit_id = var_results.id_res.unit_id

Read here about select_related which does a join.

Read here for .values() and how to access related fields.

See here for __range.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but i get: Cannot resolve keyword 'read_date' into field. Choices are: id, id_res, id_res_id, var_hash, var_id, var_id_id, var_val
Sorry my .filter() was wrong, I've updated the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.