S
S
Sama Samsonov2017-01-24 12:37:48
Django
Sama Samsonov, 2017-01-24 12:37:48

How to rewrite this in Django?

<?php
$pdo = new PDO('mysql:host=localhost;dbname=gb', 'root', '');
$select = 'SELECT *';
$from = 'FROM workers';
$where = 'WHERE TRUE';
$opts = isset($_POST['filterOpts'])? $_POST['filterOpts'] : array('');
if (in_array("hasCar", $opts)){
$where .= " AND hasCar = 1";
}
if (in_array("speaksForeignLanguage", $opts)){
$where .= " AND speaksForeignLanguage = 1";
}
if (in_array("canWorkNights", $opts)){
$where .= " AND canWorkNights = 1";
}
if (in_array("isStudent",
$opts)){ $where .= " AND isStudent = 1";
}
$sql = $select . $from. $where;
$statement = $pdo->prepare($sql);
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
?>
How to rewrite this in Django ?
How to replace $where = ' WHERE TRUE'; in django?
How to replace if (in_array("speaksForeignLanguage", $opts)) in Django ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2017-01-24
@petermzg

In django you use an ORM, but in it you already operate with models.
' WHERE TRUE' is not needed at all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question