How do I select from a subquery using the Django ORM?

时间:2016-02-12 21:23:46

标签: python mysql django django-models

I have a table that is meant to be insert only. It has columns for id, object_id, and user_id. When you update a record, instead of updating the row, you create a new record with a matching object_id.

I'm trying to pull all records that match a given user_id with the highest id for each individual object_id.

I can do what I'm attempting to describe with a subquery like so:

 protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            { Allow_CheckIn(); }
        }

 private void Allow_CheckIn()
        {
            using (CheckInSystemDataContext objDataContext = new CheckInSystemDataContext())
            {
                Checkin newCheckIn = new Checkin();
                newCheckIn.VisitorName = visitor.Value.ToString();
                newCheckIn.TypeOfVisitKey = Convert.ToInt32(TypeOfVisitDesc.SelectedValue);
                newCheckIn.VisiteeName = visitee.Value.ToString();
                newCheckIn.Arrival = Convert.ToDateTime(arrival.Value);
                newCheckIn.Departure = Convert.ToDateTime(departure.Value);

                objDataContext.CheckIns.InsertOnSubmit(newCheckIn);
                objDataContext.SubmitChanges();

                Status.Text = "Check-In successful!";
                Status.CssClass = "success";

                Bind_gvw_CheckIn();
            }
        }

I've tried using the .raw() method, but it returns a RawQuerySet object and I'm trying to feed it to a form that needs a QuerySet.

I'd ideally like to get rid of the .raw() and just use the Django ORM, but if that isn't possible, how can I convert the RawQuerySet to a regular QuerySet?

2 个答案:

答案 0 :(得分:1)

Please try this:

<div>
  <figure class="landscape">
    <img src="http://placehold.it/750x500/e8117f/ffffff" alt="">
    <figcaption>
      Image Caption
    </figcaption>
  </figure>
  <figure class="portrait">
    <img src="http://placehold.it/300x500/efc63e/ffffff" alt="">
    <figcaption>
      Image Caption
    </figcaption>
  </figure>
  <figure class="portrait">
    <img src="http://placehold.it/300x500/efc63e/ffffff" alt="">
    <figcaption>
      Image Caption
    </figcaption>
  </figure>
  <figure class="landscape">
    <img src="http://placehold.it/750x500/e8117f/ffffff" alt="">
    <figcaption>
      Image Caption
    </figcaption>
  </figure>
</div>

答案 1 :(得分:1)

如果我说对了你的桌子结构是:

----------------------------
|           Table          |
----------------------------
| id | user_id | object_id |
----------------------------
| 1  |   100   |    10     |
----------------------------
| 2  |   100   |    20     |
----------------------------
| 3  |   200   |    80     |
----------------------------
| 4  |   100   |    80     |
----------------------------
| 5  |   100   |    10     |
----------------------------

您的查询结果应为:

----------------------------
|           Table          |
----------------------------
| id | user_id | object_id |
----------------------------
| 4  |   100   |    80     |
----------------------------
| 2  |   100   |    20     |
----------------------------
| 5  |   100   |    10     |
----------------------------

然后尝试distinct(),如下:

Model.objects.filter(user_id=100).order_by('-object_id', '-id').distinct('object_id')

这应该返回QuerySet,其中user_id等于100的记录,然后按object_id排序,id秒按降序排序,并由{{1}排序} distinct只会显示每个相同object_id的第一条记录,其中指定的排序将是object_id最高的记录。