Sunday 15 July 2012

python - Complex Query: Select latest object from every day, or if present, a flagged object -


OK, so I'm working with a blog (Zinnie) and there are model entries. Special query should be prepared:

  1. Select at most one entry per day.
  2. If an entry is marked "special", then select it
  3. otherwise select the latest

    aligned model:

      class EntryAbstractClass (models.Model): "" "Designed for base model publishing entries" "" creation_date = models.DateTimeField (_ ('creation date'), default = timezone.now) start_publication = models.DateTimeField End_publication = models.DateTimeField (_ ('end publication'), empty = true, zero = true (_ ('start publication'), empty = true, zero = true, help_text = _ ('date start publish')) , Help_text = _ ('Date end published')) = m Dal. BooleanField (_ ('special'), default = false)   

    To make things even more complex, the entry date used for "latest" is:

      if start_publication! = None: latest_data = start_publication Other: latest_data = create_data   

    I'm not sure how I will do this in SQL, very few DJanga filters. any idea?

    Hmm, maybe using Q object? I'm not sure that I believe 100%, see this:

    How to do something like this (completely unprotected):

      datetime import datetime to django.db Model Import Q filter = Q () # If there are no featured filters == (Feature = True) # Or if it was the "Publish" filter today = Q (start_publication__day = datetime.today ()) # or if it was "made" filter today. = Q (creation_date__day = datetime.today ()) # one entry_of_de_de_yyll = entries.obages Filter (filter) [: 1]    

No comments:

Post a Comment