|
List Info
Thread: How to select all the values from my Field attribute 'choices?
|
|
| How to select all the values from my
Field attribute 'choices? |
  United States |
2007-10-23 14:32:40 |
Hello,
When I first developed my application I created a table that
stored
all the different types of materials
class Material(models.Model):
material = models.CharField(maxlength=100)
To get all the materials in the table all I needed to do was
query the
table. Recently, I've been going through my code to see if
I can
clean it up. So I deleted my table Material and made it
into a
'choices' attribute. So now I have
class Collection(models.Model):
THE_MATERIAL = (
('1', 'Paper'),('2', 'Plastic'),('3', 'Other'),
)
material = models.CharField(max_length=50,
choices=THE_MATERIAL)
///////////////////
However, now I want to be able to do a query that returns
all of the
values in THE_MATERIAL (even if it's not being used by any
collection). How would the query be setup to do this?
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-unsubscribe googlegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: How to select all the values from my
Field attribute 'choices? |

|
2007-10-23 18:06:40 |
|
On 10/23/07, Greg < gms3651 hotmail.com">gms3651 hotmail.com> wrote:
[snip]
class Collection(models.Model):
THE_MATERIAL = ( (9;1', 'Paper'),(9;2', 'Plastic'),('3', 'Other'), )
material = models.CharField(max_length=50, choices=THE_MATERIAL)
///////////////////
However, now I want to be able to do a query that returns all of the values in THE_MATERIAL (even if it's not being used by any collection). How would the query be setup to do this?
Well, you can't set up a query for something that isn't in the
database. But the values are right there in THE_MATERIAL. If you want
a list of just the text values, stripping away the numeric choices, some simple Python:
material_list = [y for x,y in THE_MATERIAL]
would do it.
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 django-users-unsubscribe googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---
|
| Re: How to select all the values from my
Field attribute 'choices? |
  United States |
2007-11-06 08:06:31 |
Hello Karen
I have the same problem as Greg has.
If I try to use your code I got an "page not
found" error.
Here is my code:
views.py
### CODE ###
def Karateka_edit(request, karateka_id):
from kav.info.models import Karateka
try:
karateka = Karateka.objects.get(id = karateka_id)
roles = [y for x,y in ROLE]
except Karateka.DoesNotExist:
raise Http404
return render_to_response('info_karateka_edit.html', {
'karateka' :
karateka,
'roles' :
roles,
'user' :
request.user})
### END CODE ###
Do I need to import ROLE like Karateka or do I have an other
error in
my code?
Regards,
äL
On 24 Okt., 00:06, "Karen Tracey" <kmtra... gmail.com> wrote:
> On 10/23/07, Greg <gms3... hotmail.com> wrote:
> [snip]
>
> > class Collection(models.Model):
>
> > THE_MATERIAL = (
> > ('1', 'Paper'),('2', 'Plastic'),('3',
'Other'),
> > )
>
> > material = models.CharField(max_length=50,
choices=THE_MATERIAL)
>
> > ///////////////////
>
> > However, now I want to be able to do a query that
returns all of the
> > values in THE_MATERIAL (even if it's not being
used by any
> > collection). How would the query be setup to do
this?
>
> Well, you can't set up a query for something that isn't
in the database.
> But the values are right there in THE_MATERIAL. If you
want a list of just
> the text values, stripping away the numeric choices,
some simple Python:
>
> material_list = [y for x,y in THE_MATERIAL]
>
> would do it.
>
> 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
django-users-unsubscribe googlegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: How to select all the values from my
Field attribute 'choices? |

|
2007-11-06 14:27:58 |
|
On 11/6/07, äL < grippi gmx.ch">grippi gmx.ch> wrote:
Hello Karen
I have the same problem as Greg has.
If I try to use your code I got an "page not found" error. Here is my code:
views.py ### CODE ### def Karateka_edit(request, karateka_id):
from kav.info.models import Karateka
try: karateka = Karateka.objects.get(id = karateka_id) roles = [y for x,y in ROLE] except Karateka.DoesNotExist: raise Http404
return render_to_response(39;info_karateka_edit.html', { 'karateka' : karateka, 'roles' : roles,
'user' : request.user}) ### END CODE ###
Do I need to import ROLE like Karateka or do I have an other error in my code? Yes, assuming ROLE is defined in your models file you will need to import it when you want to use it from another file. But that is not why you got a 404 (page not found). Given that code, you'd only get a 404 if the
Karateka.objects.get call raises Karateka.DoesNotExist. So you didn't even get as far as the roles= line (there';s no reason to include that in the try/except btw). If you had gotten as far as the roles= line and if you had not imported ROLE from wherever it is defined, you would have gotten a NameError "Global name ROLE is not defined".
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 django-users-unsubscribe googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---
|
[1-4]
|
|