Re: SQL statement runs in a loop
On 4 Feb., 11:48, Daniel Roseman wrote: > On Feb 4, 6:23 am, äL wrote: > > > I have an SQL statement in views.py: > > > karatekas = Karateka.objects.extra(where = ["bsc = 1 OR skr = > > 1"]).select_related() > > > This statement runs in a loop an no data will come back. > > > If I change the little word "OR" to "AND", > > > karatekas = Karateka.objects.extra(where = ["bsc = 1 AND skr = > > 1"]).select_related() > > > the right data will show up. > > > Why does it not work with "OR"? > > What do you mean by 'runs in a loop'? I can't parse that sentence. > What is the actual behaviour? Do you get an error? The browser seems to load and load an load. But no data will come up. No, I don't get any error. > > I'm not sure why you're doing this in SQL, anyway. This can be done > directly in the ORM: > Karateka.objects.filter(Q(bsc=1) | Q(skr=1)).select_related() I tried with your example. Now it works how I want it. Thanks a lot for your help. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.
SQL statement runs in a loop
I have an SQL statement in views.py: karatekas = Karateka.objects.extra(where = ["bsc = 1 OR skr = 1"]).select_related() This statement runs in a loop an no data will come back. If I change the little word "OR" to "AND", karatekas = Karateka.objects.extra(where = ["bsc = 1 AND skr = 1"]).select_related() the right data will show up. Why does it not work with "OR"? -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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: How to select all the values from my Field attribute 'choices?
How can I get all values of my "choices"? I have a template which contains an drop-down-list with all values. The drop-down-list has several entries but unfortunatelly only the same. In my choices- list I have 5 entries. And in the drop-down-list 5 entries, too (But the same). How can I get all values? From a table it's easy: Just type "nations = Country.objects.all()" With a choice-list "objects.all()" doesn't work. äL On 8 Nov., 13:42, "Marty Alchin" <[EMAIL PROTECTED]> wrote: > On 11/8/07, äL <[EMAIL PROTECTED]> wrote: > > > Do I have an error in the import syntax? How can I import ROLE? > > You don't. Just import Karateka and use Karateka.ROLE. > > -Gul --~--~-~--~~~---~--~~ 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: How to select all the values from my Field attribute 'choices?
Hi, If I try to import ROLE I get an the error "cannot import name ROLE" views.py ### CODE ### def Karateka_edit(request, karateka_id): from kav.info.models import Karateka, ROLE roles= [y for x,y in ROLE] try: karateka = Karateka.objects.get(id = karateka_id) except Karateka.DoesNotExist: raise Http404 return render_to_response('info_karateka_edit.html', { 'karateka' : karateka, 'roles' : roles, 'user' : request.user}) ### END CODE ### Here is my models.py ### CODE ### class Karateka(models.Model): ROLE = ( ('1', 'Sportler'), ('2', 'Nachwuchsteam A'), ('3', 'Nachwuchsteam B'), ('4', 'Funktionär'), ('5', 'Trainer'), ) person= models.ForeignKey (Person,verbose_name = 'Person', core = True) role = models.CharField('Funktion',blank = True, null = True, maxlength = 1, choices=ROLE) def __str__(self): return "%s" % (self.person) class Admin: list_display = ('person') fields = ( (None, {'fields': ('person', 'bsc')}), ) def name_last(self): return "%s" % (self.person.nameLast) class Meta: ordering = ('person',) verbose_name= 'Karateka' verbose_name_plural = 'Karatekas' ### END CODE ### Do I have an error in the import syntax? How can I import ROLE? Thanks for your help regards, äL On 6 Nov., 21:27, "Karen Tracey" <[EMAIL PROTECTED]> wrote: > On 11/6/07, äL <[EMAIL PROTECTED]> 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('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 [EMAIL PROTECTED] 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?
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" <[EMAIL PROTECTED]> wrote: > On 10/23/07, Greg <[EMAIL PROTECTED]> 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Customize Admin Object elements
I would like to do the same. But if I try class Meta: ordering = ['name'] the drop down menu is still ordered by ID and not by name. Because name is in an other model (foreign key). This is my code: ### CODE ### class Person(models.Model): nameLast = models.CharField ('Nachname', maxlength = 31) nameFirst = models.CharField ('Vorname', maxlength = 31) address = models.CharField ('Adresse', maxlength = 63) def __str__(self): return "%s, %s " % (self.nameLast, self.nameFirst) class Admin: list_display = ('nameLast', 'nameFirst', 'address') fields = ( ('Personalien',{'fields': ('nameLast', 'nameFirst', 'address')}) ) class Meta: ordering = ('nameLast', 'nameFirst', 'location') verbose_name= "Person" verbose_name_plural = "Personen" class Karateka(models.Model): person= models.ForeignKey (Person, verbose_name = 'Person', core = True) bsc = models.BooleanField ('BSC') comment = models.TextField('Bemerkung', blank = True, null = True) def __str__(self): return "%s" % (self.person) class Admin: list_display = ('person') fields = ( (None, {'fields': ('person')}) ) def name_last(self): return "%s" % (self.person.nameLast) class Meta: ordering= ('person') # I need to order by "person.nameLast" verbose_name= 'Karateka' verbose_name_plural = 'Karatekas' ### END CODE ### If I try "ordering = ('person.nameLast')" I get the error "Column not found". In templates this problem is resolvable. But how can I get an ordered drop down list in the admin interface? On 27 Okt., 21:23, Griffin Caprio <[EMAIL PROTECTED]> wrote: > Gustaf, > > Awesome thanks. > > - Griffin > On Oct 27, 2007, at 2:15 PM, [EMAIL PROTECTED] wrote: > > > > > class MyModel(models.Model): > > ... > > > class Meta: > > ordering = ['name'] > > > /Gustaf > > > On Oct 27, 8:07 pm, Griffin <[EMAIL PROTECTED]> wrote: > >> Is there any way to customize thedropdownlists that appear when > >> editing an object in theadmininterface? I would like to change the > >>orderthat elements appear. Instead of sorting by id, I would > >> like to > >> sort by another property such as name. > > >> I've already customized theAdminclass within the model itself, > >> but I > >> can't find any option that allows me to customize how the fields are > >> displayed. > > >> Is the only option a custom view? > > >> Thanks, > >> Griffin --~--~-~--~~~---~--~~ 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: save() datefield
No, I do not get any traceback. If any statement fails the exeption will redirect me back to the edit form. How can I force a traceback? It would be more helpfully if an error message will be returned. I will check soon if I can solve the problem with your hints. On 26 Okt., 14:01, Thomas Guettler <[EMAIL PROTECTED]> wrote: > Am Freitag, 26. Oktober 2007 10:53 schrieb äL: > > > I use Django to manage addresses. To publish the data I have a list > > and a detailed view. In the detail view I can change the name, > > address, phone, etc. of the person. To do that I created an edit > > form. > > > Now, everythin is fine. Only if I try to change and save the birthday > > (witch is a datefield) it doesn't work. Everthing else is no problem. > > Does anybody know why I cannot save datefields? > > What mean "doesn't work"? Nothings happens? Do you get a traceback? > > [cut] > > > > > ## views.py ## > > def Karateka_save(request, karateka_id): > > from kav.info.models import Karateka, Country, Location, Dojo > > karateka = Karateka.objects.get(id = karateka_id) > > > try: > > karateka.person.nameLast = request['nameLast'] > > karateka.person.nameFirst = request['nameFirst'] > > karateka.person.address = request['address'] > > karateka.person.phone = request['phone'] > > karateka.person.birthdate = request['birthdate'] > > karateka.comment = request['comment'] > > > karateka.save() > > karateka.person.save() > > except: > > return HttpResponseRedirect("/info/karateka-%d/edit" % > > int(karateka_id)) > > > return HttpResponseRedirect("/info/karateka-%d/" % > > int(karateka_id) ) > > ## end views.py ## > > this looks very strange. I guess you are new to python programming. > > Some hints: > > - don't catch all exceptions ("except:") > - keep only few (at best one line) between "try" and "except". > - birthday is of type datetime.date. You set it to a string. > - Have a look at newforms. form_for_model and form_for_instance > might help you. You should use forms.DateField for the birthday. > > Thomas --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
save() datefield
I use Django to manage addresses. To publish the data I have a list and a detailed view. In the detail view I can change the name, address, phone, etc. of the person. To do that I created an edit form. Now, everythin is fine. Only if I try to change and save the birthday (witch is a datefield) it doesn't work. Everthing else is no problem. Does anybody know why I cannot save datefields? Here is my code: ### CODE ### ## models.py ## class Person(models.Model): nameLast = models.CharField ('Nachname', maxlength = 31) nameFirst = models.CharField ('Vorname', maxlength = 31) address = models.CharField ('Adresse', maxlength = 63) birthdate = models.DateField ('Geburtsdatum', blank = True, null = True,) phone = models.CharField ('Telefon', blank = True, null = True, maxlength = 47) def __str__(self): return "%s, %s " % (self.nameLast, self.nameFirst) class Admin: list_display = ('nameLast', 'nameFirst', 'address', 'location') fields = ( ('Personalien',{'fields': ('nameLast', 'nameFirst', 'address', 'location', 'birthdate', 'nation')}), ) class Meta: ordering = ('nameLast', 'nameFirst',) verbose_name= "Person" verbose_name_plural = "Personen" class Karateka(models.Model): person= models.ForeignKey (Person, verbose_name = 'Person', core = True) comment = models.TextField('Bemerkung', blank = True, null = True) def __str__(self): return "%s" % (self.person) class Admin: list_display = ('person', 'bsc', 'skr', 'dojo') def name_last(self): return "%s" % (self.person.nameLast) class Meta: ordering = ('person',) verbose_name= 'Karateka' verbose_name_plural = 'Karatekas' ## end models.py ## ## views.py ## def Karateka_save(request, karateka_id): from kav.info.models import Karateka, Country, Location, Dojo karateka = Karateka.objects.get(id = karateka_id) try: karateka.person.nameLast = request['nameLast'] karateka.person.nameFirst = request['nameFirst'] karateka.person.address = request['address'] karateka.person.phone = request['phone'] karateka.person.birthdate = request['birthdate'] karateka.comment = request['comment'] karateka.save() karateka.person.save() except: return HttpResponseRedirect("/info/karateka-%d/edit" % int(karateka_id)) return HttpResponseRedirect("/info/karateka-%d/" % int(karateka_id) ) ## end views.py ## --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Ordering by foreign key, class Meta
I would like to order a list in a view by foreign key. If I try ordering how in the code below my list is ordered by 'person'. And this means that the list ist ordered by the ID of the table Person. But I need a list ordered by 'nameLast'. Thus I changed ordering 'person' into 'person.nameLast'. But then I got an error "Column not found". Does anybody have an ideia how to order by a foreign key? I spent a lot of time for searching any solution. Unfortunatelly without success. ### CODE ### class Person(models.Model): nameLast = models.CharField ('Nachname', maxlength = 31) nameFirst = models.CharField ('Vorname', maxlength = 31) address = models.CharField ('Adresse', maxlength = 63) def __str__(self): return "%s, %s " % (self.nameLast, self.nameFirst) class Admin: list_display = ('nameLast', 'nameFirst', 'address') fields = ( ('Personalien',{'fields': ('nameLast', 'nameFirst', 'address')}) ) class Meta: ordering = ('nameLast', 'nameFirst', 'location') verbose_name= "Person" verbose_name_plural = "Personen" class Karateka(models.Model): person= models.ForeignKey (Person, verbose_name = 'Person', core = True) bsc = models.BooleanField ('BSC') comment = models.TextField('Bemerkung', blank = True, null = True) def __str__(self): return "%s" % (self.person) class Admin: list_display = ('person') fields = ( (None, {'fields': ('person')}) ) def name_last(self): return "%s" % (self.person.nameLast) class Meta: ordering= ('person') verbose_name= 'Karateka' verbose_name_plural = 'Karatekas' ### END CODE ### --~--~-~--~~~---~--~~ 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: Check if user is a member of group in template
Great. Now it works how I expected. See also http://www.djangoproject.com/documentation/templates_python/#extending-the-template-system for further information. On 9 Okt., 21:38, Ryan Kanno <[EMAIL PROTECTED]> wrote: > I think you're looking for something like this: > > http://www.djangosnippets.org/snippets/282/ > > On Oct 9, 3:22 am, äL <[EMAIL PROTECTED]> wrote: > > > Inhttp://code.djangoproject.com/wiki/CookBookRequiredGroupLoginI > > found how > > to show a template only if the user is in a specific group (admin). > > > Now I would like to do the same in a template. How can I hide a link, > > for example, > > if the user is not in the admin group? I'm looking for something like > > this: > > > {% ifequal user.group "admin" %} > > Show detail > > {% else %} > > > > {% endifequal %} > > > Is there a django-command to achive this aim? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Check if user is a member of group in template
In http://code.djangoproject.com/wiki/CookBookRequiredGroupLogin I found how to show a template only if the user is in a specific group (admin). Now I would like to do the same in a template. How can I hide a link, for example, if the user is not in the admin group? I'm looking for something like this: {% ifequal user.group "admin" %} Show detail {% else %} {% endifequal %} Is there a django-command to achive this aim? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---