Re: get_FOO_display not working ?

2020-02-23 Thread Yves de Champlain
Hi

I’ll know if it is much help if it solves my problem. I’ll have a look into 
that.

Thanks !

yves


> Le 23 févr. 2020 à 02:32, Mike Dewhirst  a écrit :
> 
> Maybe the choices should be
> 
> STATUS = [
> ('draft', _('Draft')),
> ...
> ]
> 
> I think the migration system might detect a change in the model every time it 
> is run because the get_text_lazy() function can return a non-static result. 
> Not sure about that.
> 
> I haven't used _() myself but I have used methods to fill up choices lists 
> with just that outcome. I had to finalize the list prior to running migrate.
> 
> Not much help I know
> 
> Good luck
> 
> Mike
> 
>  Original message 
> From: Yves de Champlain 
> Date: 23/2/20 15:47 (GMT+10:00)
> To: Django users 
> Subject: get_FOO_display not working ?
> 
> Hi
> 
> I'm using StatusModel from models_utils :
> 
> class MetaData(TimeStampedModel, StatusModel, SoftDeletableModel):
> STATUS = Choices(('draft', _('Draft')),
>  ('submitted', _('Submitted')),
>  ('underreview', _('Underreview')),
>   )
> 
> Forms using STATUS work as expected, but when I load an object from the DB, I 
> can't access the human readable data in my templates.
> 
> a.status => draft
> a.get_status_display => draft
> 
> instead of
> 
> a.status => draft
> a.get_status_display => Draft
> 
> Thanks for any hep on this !
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to django-users+unsubscr...@googlegroups.com 
> .
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/d7a06913-39a8-4e55-9799-ebc46cb5cde4%40googlegroups.com
>  
> .
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to django-users+unsubscr...@googlegroups.com 
> .
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/5e522a9f.1c69fb81.cae91.b7f5SMTPIN_ADDED_MISSING%40gmr-mx.google.com
>  
> .

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/377B8ADD-1C7A-4418-BA76-3A12C325DC44%40gmail.com.


Re: get_FOO_display not working ?

2020-02-23 Thread Yves de Champlain
Hi

Actually, the human readable value is not (‘Draft’) but _(‘Draft’) which is 
shorthand for gettext_lazy(‘Draft’)

Yves


> Le 22 févr. 2020 à 23:54, Nde Nguti  a écrit :
> 
> CHOICES =(('draft', 'Draft'), )
> 
> On Sun, Feb 23, 2020, 05:48 Yves de Champlain  > wrote:
> Hi
> 
> I'm using StatusModel from models_utils :
> 
> class MetaData(TimeStampedModel, StatusModel, SoftDeletableModel):
> STATUS = Choices(('draft', _('Draft')),
>  ('submitted', _('Submitted')),
>  ('underreview', _('Underreview')),
>   )
> 
> Forms using STATUS work as expected, but when I load an object from the DB, I 
> can't access the human readable data in my templates.
> 
> a.status => draft
> a.get_status_display => draft
> 
> instead of
> 
> a.status => draft
> a.get_status_display => Draft
> 
> Thanks for any hep on this !
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to django-users+unsubscr...@googlegroups.com 
> .
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/d7a06913-39a8-4e55-9799-ebc46cb5cde4%40googlegroups.com
>  
> .
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to django-users+unsubscr...@googlegroups.com 
> .
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/CALfkE84BbTc47u0TGL_PWYNXpoXjs0rsZUG4rjSBNySd8SLGyw%40mail.gmail.com
>  
> .

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/C1C43B13-5D76-4C44-9979-F6D9E12D5F6F%40gmail.com.


RE: get_FOO_display not working ?

2020-02-22 Thread Mike Dewhirst
Maybe the choices should beSTATUS = [    ('draft', _('Draft')),    ...]I think 
the migration system might detect a change in the model every time it is run 
because the get_text_lazy() function can return a non-static result. Not sure 
about that.I haven't used _() myself but I have used methods to fill up choices 
lists with just that outcome. I had to finalize the list prior to running 
migrate.Not much help I knowGood luckMike
 Original message From: Yves de Champlain  
Date: 23/2/20  15:47  (GMT+10:00) To: Django users 
 Subject: get_FOO_display not working ? HiI'm 
using StatusModel from models_utils :class MetaData(TimeStampedModel, 
StatusModel, SoftDeletableModel):    STATUS = Choices(('draft', _('Draft')),    
                 ('submitted', _('Submitted')),                     
('underreview', _('Underreview')),                      )Forms using STATUS 
work as expected, but when I load an object from the DB, I can't access the 
human readable data in my templates.a.status => drafta.get_status_display => 
draftinstead ofa.status => drafta.get_status_display => DraftThanks for any hep 
on this !



-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/d7a06913-39a8-4e55-9799-ebc46cb5cde4%40googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/5e522a9f.1c69fb81.cae91.b7f5SMTPIN_ADDED_MISSING%40gmr-mx.google.com.


Re: get_FOO_display not working ?

2020-02-22 Thread Nde Nguti
CHOICES =(('draft', 'Draft'), )

On Sun, Feb 23, 2020, 05:48 Yves de Champlain  wrote:

> Hi
>
> I'm using StatusModel from models_utils :
>
> class MetaData(TimeStampedModel, StatusModel, SoftDeletableModel):
> STATUS = Choices(('draft', _('Draft')),
>  ('submitted', _('Submitted')),
>  ('underreview', _('Underreview')),
>   )
>
> Forms using STATUS work as expected, but when I load an object from the
> DB, I can't access the human readable data in my templates.
>
> a.status => draft
> a.get_status_display => draft
>
> instead of
>
> a.status => draft
> a.get_status_display => Draft
>
> Thanks for any hep on this !
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/d7a06913-39a8-4e55-9799-ebc46cb5cde4%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CALfkE84BbTc47u0TGL_PWYNXpoXjs0rsZUG4rjSBNySd8SLGyw%40mail.gmail.com.


Re: get_FOO_display not working ?

2020-02-22 Thread Nde Nguti
Try
('draft', 'Draft') not ('draft', ('Draft'))




On Sun, Feb 23, 2020, 05:48 Yves de Champlain  wrote:

> Hi
>
> I'm using StatusModel from models_utils :
>
> class MetaData(TimeStampedModel, StatusModel, SoftDeletableModel):
> STATUS = Choices(('draft', _('Draft')),
>  ('submitted', _('Submitted')),
>  ('underreview', _('Underreview')),
>   )
>
> Forms using STATUS work as expected, but when I load an object from the
> DB, I can't access the human readable data in my templates.
>
> a.status => draft
> a.get_status_display => draft
>
> instead of
>
> a.status => draft
> a.get_status_display => Draft
>
> Thanks for any hep on this !
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/d7a06913-39a8-4e55-9799-ebc46cb5cde4%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CALfkE84yXRmXK-34WLxHq_yVMWMf8ngghORVuX4GXdzeXk6f8g%40mail.gmail.com.


Re: get_FOO_display() for generic/variable choice fields

2012-01-27 Thread Michael Elkins

On Tue, Jan 24, 2012 at 03:51:27AM -0800, katstevens wrote:

Obviously the 'val = obj.get_field_display()' line doesn't work!

Is there a generic way of doing this (something like
obj.get_display(fieldname) ?) or am I going to have to hard code
separate checks for get_country_display() and
get_dialing_code_display()?


I think you can do what you want like this:

obj = queryset[my_item]

for field in obj._meta.fields:
if field.choices:
val = obj._get_FIELD_display(field)
else:
val = getattr(obj, field.name)
row.append(val)

--
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: get_FOO_display() for generic/variable choice fields

2012-01-25 Thread katstevens
My Python-expert partner suggested a clever workaround as follows:

As I'm already running through the model._meta.fields beforehand to
create header row for my .csv file, I can build a dictionary of the
choices to refer to later.

choices_lookup_dict = {}
header_row = []

for field in model._meta.fields:
  # create header row for csv file
  header_row.append(field.name)
  # Add lookup dictionary of choices to dictionary of fields
  #  i.e. creating a dictionary of dictionaries for each choice field
  if field.choices:
choices_lookup_dict[field.name] = dict(field.choices)

This could be quite expensive for large choice tuples such as country
lists, but at least we're only calling it once.

So now in my main loop I can use the following code to lookup my
verbose choice value:


for field in model._meta.fields:
   val = getattr(obj, field.name)
   if field.choices:
 val = choices_lookup_dict[field.name][val]
   row.append(val)


We assume that get_FOO_display() must be doing something like this
anyway!

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: get_FOO_display() for generic/variable choice fields

2012-01-24 Thread Bill Freeman
Maybe try:

val = getattr(obj, 'get_%s_display' % field.name)()


On 1/24/12, katstevens  wrote:
> I have a model ContactDetail with two choice fields,
> ContactDetail.dialing_code and ContactDetail.country. These use the
> standard tuples ('AU', 'Australia') and so on.
>
> I know I can use get_country_display() and get_dialing_code_display()
> to show the longer values, but is there a more generic version where
> the field name is replaced with a variable?
>
> For example, I want to create a customised .csv file from the model
> data. I can loop through the fields from my model and get the shorter
> attribute values without any problems. However I'd like to do
> something like this:
>
>   obj = queryset[my_item] # an instance of ContactDetail()
>   model = queryset.model # ContactDetail() itself
>
>   for field in model._meta.fields:
>   val = getattr(obj, field.name)  # gets the shorter version
>   if field.choices:# if field is ChoiceField, 
> convert data
> to display mode
>   val = obj.get_field_display()
>   row.append(val)
>
> Obviously the 'val = obj.get_field_display()' line doesn't work!
>
> Is there a generic way of doing this (something like
> obj.get_display(fieldname) ?) or am I going to have to hard code
> separate checks for get_country_display() and
> get_dialing_code_display()?
>
> Ideally I would like to use this particular function for other models
> and keep the fields as generic as possible.
>
> --
> 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
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: get_FOO_display

2011-12-13 Thread Mike Dewhirst
Absolutely. I went back and found where I had made it work previously and that 
is exactly what I did. I just had a (hopefully) temporary blur. 

Thanks

Mike

On 13/12/2011, at 8:47 PM, Ilian Iliev  wrote:

> Or you can change your field to IntegerField instead of CharField if you are 
> planning to have only Integer
> values for the choices.
> 
> 
> -- 
> eng. Ilian Iliev
> Web Software Developer
> 
> Mobile: +359 88 66 08 400
> Website: http://ilian.i-n-i.org
> 
> 
> On Tue, Dec 13, 2011 at 9:53 AM, Mike Dewhirst  wrote:
> On 13/12/2011 5:21pm, kenneth gonsalves wrote:
> On Tue, 2011-12-13 at 17:16 +1100, Mike Dewhirst wrote:
> THINGS = ( (0, 'Thing Zero'),
> (1, 'Thing One'), )
> 
> what happens when you do:
> 
> THINGS = ( ('0', 'Thing Zero'),
> ('1', 'Thing One'), )
> 
> 
> By golly it worked! Must have missed that in the docs.
> 
> Thanks Kenneth
> 
> Mike
> 
> 
> -- 
> 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 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/django-users?hl=en.
> 
> 
> 
> -- 
> 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 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/django-users?hl=en.

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: get_FOO_display

2011-12-13 Thread Ilian Iliev
Or you can change your field to IntegerField instead of CharField if you
are planning to have only Integer
values for the choices.


-- 
eng. Ilian Iliev
Web Software Developer

Mobile: +359 88 66 08 400
Website: http://ilian.i-n-i.org


On Tue, Dec 13, 2011 at 9:53 AM, Mike Dewhirst wrote:

> On 13/12/2011 5:21pm, kenneth gonsalves wrote:
>
>> On Tue, 2011-12-13 at 17:16 +1100, Mike Dewhirst wrote:
>>
>>> THINGS = ( (0, 'Thing Zero'),
>>> (1, 'Thing One'), )
>>>
>>
>> what happens when you do:
>>
>> THINGS = ( ('0', 'Thing Zero'),
>> ('1', 'Thing One'), )
>>
>
>
> By golly it worked! Must have missed that in the docs.
>
> Thanks Kenneth
>
> Mike
>
>
> --
> 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 django-users+unsubscribe@**
> googlegroups.com .
> For more options, visit this group at http://groups.google.com/**
> group/django-users?hl=en
> .
>
>

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: get_FOO_display

2011-12-12 Thread Mike Dewhirst

On 13/12/2011 5:21pm, kenneth gonsalves wrote:

On Tue, 2011-12-13 at 17:16 +1100, Mike Dewhirst wrote:

THINGS = ( (0, 'Thing Zero'),
 (1, 'Thing One'), )


what happens when you do:

THINGS = ( ('0', 'Thing Zero'),
 ('1', 'Thing One'), )



By golly it worked! Must have missed that in the docs.

Thanks Kenneth

Mike

--
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: get_FOO_display

2011-12-12 Thread kenneth gonsalves
On Tue, 2011-12-13 at 17:16 +1100, Mike Dewhirst wrote:
> THINGS = ( (0, 'Thing Zero'),
> (1, 'Thing One'), ) 

what happens when you do:

THINGS = ( ('0', 'Thing Zero'),
('1', 'Thing One'), ) 
-- 
regards
Kenneth Gonsalves

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: get_FOO_display

2009-02-06 Thread Alex Koshelev
get_FOO_display is an instance method not field. So try this:

{{ object.get_invoice_type_display }}



On Fri, Feb 6, 2009 at 6:09 PM, Alfonso  wrote:

>
> I've got a field called 'invoice_type' which is a ChoiceField.
>
> In my template I'm trying to pull out the 'humanized' invoice type by
> using {{ object.invoice_type.get_invoice_type_display }} but django's
> not happy.  I'm guessing it's because of the invoice[underscore]type
> syntax I used?
>
> Any workarounds?
>
> Thanks
> >
>

--~--~-~--~~~---~--~~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: get_FOO_display() and Update Messages

2008-05-03 Thread Karen Tracey
On Sat, May 3, 2008 at 2:15 AM, Greg Taylor <[EMAIL PROTECTED]> wrote:

>
> I was wondering if there's any reason why get_FOO_display() calls
> aren't being evaluated when updating/adding new objects via the admin
> interface. What I mean by this is:
>
> The workout "1" was changed successfully.
>
> Instead of:
>
> The workout "Running" was changed successfully.
>
> The field in question is an IntegerField with a choices tuple defined
> that looks like:
>
> WORKOUT_TYPE_CHOICES = (
>  (0, "Biking"),
>  (1, "Running"),
> )
>
> The class' __str__ method looks like this:
> return self.get_activity_type_display()
>
> Any ideas?
>

Sound like this problem:

http://code.djangoproject.com/ticket/6853

Karen

--~--~-~--~~~---~--~~
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: get_FOO_display() and Update Messages

2008-05-03 Thread dimrub

Yay another fitness app based on django!

http://code.google.com/p/fiteat/

On May 3, 9:15 am, Greg Taylor <[EMAIL PROTECTED]> wrote:
> I was wondering if there's any reason why get_FOO_display() calls
> aren't being evaluated when updating/adding new objects via the admin
> interface. What I mean by this is:
>
> The workout "1" was changed successfully.
>
> Instead of:
>
> The workout "Running" was changed successfully.
>
> The field in question is an IntegerField with a choices tuple defined
> that looks like:
>
> WORKOUT_TYPE_CHOICES = (
>   (0, "Biking"),
>   (1, "Running"),
> )
>
> The class' __str__ method looks like this:
> return self.get_activity_type_display()
>
> Any ideas?
--~--~-~--~~~---~--~~
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: get_foo_display doesn't work with integers?

2007-12-29 Thread Florian Apolloner

I found the issue (or non issue, whatever you like ;))

get_test_char_display() calls the field's choices with a string (of
course it is a CharField), which results in the following call
(stripped down talen from django/db/models/base.py:
dict(choices).get(value,value)
Now accesing it with a string as value would raise a KeyError so the
value saved in the CharField is returned, which is a string containing
an int.

The remaining question now is whether this should get documented
somehow (eg match the type of the field...) or not. Of course it
doesn't make sense to use integers and then store them in the db, but
this question popped up a few times in irc...


On Dec 29, 10:32 pm, Florian Apolloner <[EMAIL PROTECTED]> wrote:
> > Error is in the way you use it. Please write full model and usage
> > example.
>
> As you wish :)
>
> blubb/models.py:
>
> from django.db import models
>
> # Create your models here.
> MY_CHOICES = (
> (1, '11'),
> (2, '12')
> )
> class TestModel(models.Model):
> test_blubb = models.IntegerField(choices=MY_CHOICES)
> test_char = models.CharField(choices=MY_CHOICES, max_length=100)
>
> Testcode
> ./manage.py shell
> Python 2.4.4 (#2, Aug 16 2007, 02:03:40)
> [GCC 4.1.3 20070812 (prerelease) (Debian 4.1.2-15)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> (InteractiveConsole)>>> from blubb.models import *
> >>> a = TestModel.objects.get(pk=1)
> >>> a.get_test_blubb_display()
> u'11'
> >>> a.get_test_char_display()
>
> u'2'
>
> Where the last one should be u'12' instead of u'2'. The problem seems
> to be with CharField. If I use str for all the choices it does work...
--~--~-~--~~~---~--~~
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: get_foo_display doesn't work with integers?

2007-12-29 Thread Florian Apolloner

> Error is in the way you use it. Please write full model and usage
> example.

As you wish :)

blubb/models.py:


from django.db import models

# Create your models here.
MY_CHOICES = (
(1, '11'),
(2, '12')
)
class TestModel(models.Model):
test_blubb = models.IntegerField(choices=MY_CHOICES)
test_char = models.CharField(choices=MY_CHOICES, max_length=100)

Testcode
./manage.py shell
Python 2.4.4 (#2, Aug 16 2007, 02:03:40)
[GCC 4.1.3 20070812 (prerelease) (Debian 4.1.2-15)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from blubb.models import *
>>> a = TestModel.objects.get(pk=1)
>>> a.get_test_blubb_display()
u'11'
>>> a.get_test_char_display()
u'2'


Where the last one should be u'12' instead of u'2'. The problem seems
to be with CharField. If I use str for all the choices it does work...
--~--~-~--~~~---~--~~
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: get_foo_display doesn't work with integers?

2007-12-29 Thread Alex Koshelev

Error is in the way you use it. Please write full model and usage
example.

On 29 дек, 23:54, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> apollo13 pointed this out on the IRC channel:
> If your choices use an integers, get_foo_display returns the integer
> and not the "human readable" option.
>
> For example, reworking this from the docs:
> YEAR_IN_SCHOOL_CHOICES = (
> (1, 'Freshman'),
> (2, 'Sophomore'),
> (3, 'Junior'),
> (4, 'Senior'),
> (5, 'Graduate'),
> )
>
> get_foo_display would show 1 instead of "Freshman". The select widget
> works right either way.
>
> Is this expected behavior? Or should ints work?
>
> If this is expected behavior, perhaps we need a note in the docs to
> that effect (I'd be glad to open the ticket).
--~--~-~--~~~---~--~~
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: get_FOO_display and grouped data

2006-12-04 Thread Ross Burton

Jacob Kaplan-Moss wrote:
> Try::
>
> {% regroup feature.task_set.all|dictsortreversed:"priority" by
> get_priority_display as grouped %}
>
> That is, the "by" argument to {% regroup %} doesn't just have to be a field
> name; it can be anything that a variable could resolve.

Excellent, thanks.

Ross


--~--~-~--~~~---~--~~
 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: get_FOO_display and grouped data

2006-12-04 Thread Jacob Kaplan-Moss

On 12/4/06 4:34 PM, Ross Burton wrote:
> In my view, I'm grouping the data on the priority:
> 
> {% regroup feature.task_set.all|dictsortreversed:"priority" by priority
> as grouped %}
> {% for group in grouped %}
>   {{ group.grouper }}
> 
> However, when I do this, group.grouper expands to "0" or "2", not "Low"
> or High".  How can I get the display name for this field?

Try::

{% regroup feature.task_set.all|dictsortreversed:"priority" by 
get_priority_display as grouped %}

That is, the "by" argument to {% regroup %} doesn't just have to be a field 
name; it can be anything that a variable could resolve.

Jacob

--~--~-~--~~~---~--~~
 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
-~--~~~~--~~--~--~---