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?
country flaguser name
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-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.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?
user name
2007-10-23 18:06:40
On 10/23/07, Greg < gms3651hotmail.com">gms3651hotmail.com> wrote:
[snip]
class Collection(models.Model):

 ; &nbsp; THE_MATERIAL = (
 &nbsp; &nbsp;   ; (9;1', 'Paper'),(9;2', 'Plastic'),(&#39;3', 'Other'),
&nbsp; &nbsp; )

 &nbsp; &nbsp;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-usersgooglegroups.com
To unsubscribe from this group, send email to django-users-unsubscribegooglegroups.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?
country flaguser name
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-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.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?
user name
2007-11-06 14:27:58
On 11/6/07, äL < grippigmx.ch">grippigmx.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):

&nbsp; &nbsp; from kav.info.models import Karateka

  ; &nbsp;try:
&nbsp; &nbsp; &nbsp; &nbsp; karateka&nbsp; = Karateka.objects.get(id = karateka_id)
 &nbsp; &nbsp;   ; roles &nbsp; &nbsp;= [y for x,y in ROLE]
&nbsp; &nbsp; except Karateka.DoesNotExist:
 &nbsp; &nbsp; &nbsp;  raise Http404
  ; &nbsp;return render_to_response(&#39;info_karateka_edit.html', {
 &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; 'karateka'&nbsp; : karateka,
  ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp;  'roles  ; &nbsp;  : roles,
&nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp;  'user&#39; &nbsp;   ; : 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).&nbsp; 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&quot;.

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-usersgooglegroups.com
To unsubscribe from this group, send email to django-users-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )