Re: dynamically assigning a field value

2008-01-08 Thread andy baxter

pinco wrote:
> Hi,
>
> I'm trying to figure out how to solve the following issue without
> succeed.
>
> I have a model like this:
>
> class Product(models.Model):
>...
>measure_cm = models.FloatField(...)
>measure_in = models.FloatField(...)
>   ...
>
> The fields contain the same information (a relevant dimension of a
> product) expressed in centimeter and in inches respectively.
>
>   
Why have two separate fields? Why not have one field - e.g. 'measure_mm' 
and convert to either inches or centimetres according to the user's choice?
> I would able, in the view and in the template, to do something like
> this:
>
> ...
> this_product = Product.objects.get(pk=product_id)
> this_product_measure = this_product.measure
>
> and dynamically assigning to this_product_measure the value of
> this_product.measure_cm or this_product.measure_in based on a variable
> stored in the session, representing a user preference.
>
> Actually I manage this with an "if request.session['user_preference']"
> statement, but it would be nice to catch the right value on every view
> without repeating the if statement.
>
> How can I do this?
>
> Thank you in advance
>
>
>
> >
>
>   


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



Re: dynamically assigning a field value

2008-01-05 Thread Malcolm Tredinnick


On Sat, 2008-01-05 at 04:12 -0800, pinco wrote:
> Hi,
> 
> I'm trying to figure out how to solve the following issue without
> succeed.
> 
> I have a model like this:
> 
> class Product(models.Model):
>...
>measure_cm = models.FloatField(...)
>measure_in = models.FloatField(...)
>   ...
> 
> The fields contain the same information (a relevant dimension of a
> product) expressed in centimeter and in inches respectively.
> 
> I would able, in the view and in the template, to do something like
> this:
> 
> ...
> this_product = Product.objects.get(pk=product_id)
> this_product_measure = this_product.measure
> 
> and dynamically assigning to this_product_measure the value of
> this_product.measure_cm or this_product.measure_in based on a variable
> stored in the session, representing a user preference.

For a template, you could write a context processor that puts the
computed value into the template's context each time.

For a view, you could write a method on the model that accepts the
"user" object and returns the correct value. It doesn't even have to be
a method on the model -- it could just be a separate helper function.
The point is that you're going to have to pass in the user object each
time anyway, since there's no way to magically read that out of thin
air.

Regards,
Malcolm

-- 
No one is listening until you make a mistake. 
http://www.pointy-stick.com/blog/


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



Re: dynamically assigning a field value

2008-01-05 Thread gordyt

Howdy pinco,

Off the top of my head I don't see away to avoid the test, but you
could bundle it into a method of the Product class:


def get_preferred_measure(self, request):
if request.session['user_preference'] == "cm":
return self.measure_cm
return self.measure_in


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



dynamically assigning a field value

2008-01-05 Thread pinco

Hi,

I'm trying to figure out how to solve the following issue without
succeed.

I have a model like this:

class Product(models.Model):
   ...
   measure_cm = models.FloatField(...)
   measure_in = models.FloatField(...)
  ...

The fields contain the same information (a relevant dimension of a
product) expressed in centimeter and in inches respectively.

I would able, in the view and in the template, to do something like
this:

...
this_product = Product.objects.get(pk=product_id)
this_product_measure = this_product.measure

and dynamically assigning to this_product_measure the value of
this_product.measure_cm or this_product.measure_in based on a variable
stored in the session, representing a user preference.

Actually I manage this with an "if request.session['user_preference']"
statement, but it would be nice to catch the right value on every view
without repeating the if statement.

How can I do this?

Thank you in advance



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