Django get months from models.DateTimeField
<p>Is it possible to filter a models.DateTimeField but only get the month in the filter object?</p>

<p>The field is:</p>

<pre><code>time_stamp = models.DateTimeField(
            default=timezone.now)
</code></pre>

<p>When I filter it, this is what I get:</p>

<blockquote>
  <p>[datetime.datetime(2016, 9, 22, 15, 2, 48, 867473, tzinfo=),
  datetime.datetime(2016, 9, 22, 15, 4, 22, 618675, tzinfo=),
  datetime.datetime(2016, 9, 22, 15, 5, 20, 939593, tzinfo=)]</p>
</blockquote>

<p>The filter returns 3 rows, but clearly there is too much information. I only require the months, and maybe the year.</p>

<p>How can I achieve this?</p>

<p>Any help or direction would be appreciated,</p>

<p>Thanks</p>

<p>You can use propety:</p>

<pre><code>Class your_model(models.Model):

    time_stamp = models.DateTimeField(
        default=timezone.now)

    @property
    def _get_year(self):
        return self.time_stamp.strftime("%Y-%m")

    year = property(_get_year) #EDIT
</code></pre>

python django datetime