On Tue, Oct 20, 2009 at 2:50 PM, thierry <[email protected]> wrote:
>
> Hello everybody,
>
> I'm working on a scientific project and Django has been chosen to
> develop our database model. I'd like to develop a
> 'PhysicalQuantityField' that manages a value and its relative unit.
>
> The first way to do this stuff is to translate the couple (value,unit)
> into a string and use Django custom field to manage the translation
> between Python and PostgreSQL. It works perfectly but I don't like the
> fact to mix the value and the unit. Moreover, postgres stores a string
> rather than a numeric and It makes me feel that the database model is
> not really clean.
>
> The second way, and in my mind the cleaner way, is to create a
> composite type at the postgres side called 'physicalquantity'  with
> "CREATE TYPE physicalquantity AS (value numeric,unit varchar)". It is
> possible to add data to the database, but not to search data because
> the where clause of a select query, generated by a get or filter would
> be :
>
> (with measure as a composite type)
>
> SELECT measure
> FROM myapp_mytable
> WHERE measure = '(10,"mV")'
>
> That is incorrect. The correct query would be :
>
> SELECT measure
> FROM myapp_mytable
> WHERE (measure).value = 10
> AND (measure).unit = 'mV'
>
> Can you tell me if there is a way to control the content of the where
> clause from the custom field class ?
>
> If not, do you think Django will be able one day to manage composite
> types ?
>
> Cordially,
>
> Thierry.
>
> >
>

I think your best bet would be to make 2 fields, a numeric field
(integer, decimal, float whatever you need) and a charfield or
something with the units and then just have a property in Python that
returns what you want.  Basically implement this the same way as
GenericForeignKeys.

As for whether Django will eventually support Composite data types, I
think the answer is maybe, AFAIK no one has worked on this (besides
composite primary keys which may be related implementation wise).  So
the first step would be for someone to come up with a design/API for
what it should look like.

Alex

-- 
"I disapprove of what you say, but I will defend to the death your
right to say it." -- Voltaire
"The people's good is the highest law." -- Cicero
"Code can always be simpler than you think, but never as simple as you
want" -- Me

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to