Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to deal with "partial" dates (2010-00-00) from MySQL in Django?
    primarykey
    data
    text
    <p>In one of my Django projects that use MySQL as the database, I need to have a <em>date</em> fields that accept also "partial" dates like only year (YYYY) and year and month (YYYY-MM) plus normal date (YYYY-MM-DD).</p> <p>The <em>date</em> field in MySQL can deal with that by accepting <em>00</em> for the month and the day. So <em>2010-00-00</em> is valid in MySQL and it represent 2010. Same thing for <em>2010-05-00</em> that represent May 2010.</p> <p>So I started to create a <code>PartialDateField</code> to support this feature. But I hit a wall because, by default, and Django use the default, MySQLdb, the python driver to MySQL, return a <code>datetime.date</code> object for a <em>date</em> field AND <code>datetime.date()</code> support only real date. So it's possible to modify the converter for the <em>date</em> field used by MySQLdb and return only a string in this format <em>'YYYY-MM-DD'</em>. Unfortunately the converter use by MySQLdb is set at the connection level so it's use for all MySQL <em>date</em> fields. But Django <code>DateField</code> rely on the fact that the database return a <code>datetime.date</code> object, so if I change the converter to return a string, Django is not happy at all.</p> <p>Someone have an idea or advice to solve this problem? How to create a <code>PartialDateField</code> in Django ?</p> <h2>EDIT</h2> <p>Also I should add that I already thought of 2 solutions, create 3 integer fields for year, month and day (as mention by <em>Alison R.</em>) or use a <em>varchar</em> field to keep date as string in this format <em>YYYY-MM-DD</em>.</p> <p>But in both solutions, if I'm not wrong, I will loose the <em>special</em> properties of a <em>date</em> field like doing query of this kind on them: <em>Get all entries after this date</em>. I can probably re-implement this functionality on the client side but that will not be a valid solution in my case because the database can be query from other systems (mysql client, MS Access, etc.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload