Can't execute SQL functions in app_name/sql/*.sql (IndexError: tuple index out of range)
I have two SQL functions that I'd like to use in Django. They both work no problem if I paste them into psql (PostgreSQL's shell). As I have the same problem with both, I'll just post and explain the simpler one. It looks like this: CREATE OR REPLACE FUNCTION skater_name_match(INTEGER,VARCHAR) RETURNS BOOL AS $$ SELECT $1 IN (SELECT skaters_skater.competitor_ptr_id FROM skaters_skater WHERE name||' '||surname ILIKE '%'||$2||'%' OR surname||' '||name ILIKE '%'||$2||'%'); $$ LANGUAGE SQL; and basically it takes the id of a skater and a string and checks whether the name||' '||surname of the skater or the surname||' '||name of the skater contains the string and returns true if it does. I've put this in the app_name/sql/ folder, so that it executes with the first syncdb, only it doesn't. I get an IndexError: tuple index out of range. Initially I thought maybe this had something do with the order that Django executes sql in, but I tried writing my own manage.py function which would load the custom sql and do nothing else and I got the same thing. I even tried a simple: cursor = connection.cursor() cursor.execute('''CREATE OR REPLACE FUNCTION skater_name_match (INTEGER,VARCHAR) RETURNS BOOL AS $$ SELECT $1 IN (SELECT skaters_skater.competitor_ptr_id FROM skaters_skater WHERE name||' '||surname ILIKE '%'||$2||'%' OR surname||' '||name ILIKE '%'||$2||'%'); $$ LANGUAGE SQL;''') and still the same. Could anyone give me any pointers as to what I'm doing wrong? -- 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=.
Re: forms.ChoiceField and empty values
Sorry for the double post *blush* Not sure how that happened... The model looks something like this: class Competition(MessageboardOwner): name = models.CharField(max_length=256) type = models.IntegerField(choices=COMPETITION_TYPE_CHOICES) start = models.DateField() end = models.DateField() official_www = models.URLField(blank=True) On Nov 12, 3:55 pm, rebus_ wrote: > > What does model Competition look like? > Do you have choices defined in your models.py and use choices argument > when creating that field in models? -- 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=.
Re: forms.ChoiceField and empty values
As I haven't had any replies so far, I'll add some more information... The empty label appears if I redefine the form like this: class CompetitionSearchForm(forms.ModelForm): class Meta: model = Competition fields = ('name','type') It disappears again if I make the changes I need to on the fields (i.e. "unrequire" them): class CompetitionSearchForm(forms.ModelForm): name = forms.CharField(required=False) type = forms.ChoiceField(choices=COMPETITION_TYPE_CHOICES, required=False) class Meta: model = Competition fields = ('name','type') Should I be defining my type field somehow differently? Or is this a bug in Django that I should report? -- 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=.
Re: forms.ChoiceField and empty values
As I haven't had any replies so far, I'll add some more information... The empty label appears if I redefine the form like this: class CompetitionSearchForm(forms.ModelForm): class Meta: model = Competition fields = ('name','type') It disappears again if I make the changes I need to on the fields (i.e. "unrequire" them): class CompetitionSearchForm(forms.ModelForm): name = forms.CharField(required=False) type = forms.ChoiceField(choices=COMPETITION_TYPE_CHOICES, required=False) class Meta: model = Competition fields = ('name','type') Should I be defining my type field somehow differently? Or is this a bug in Django that I should report? -- 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=.
Re: TemplateSyntaxError
When you execute manage.py sql wkw, the output is just how your current models translate into SQL. To see what's actually in the database you should do manage.py dbshell and then use whatever command your database supports (\d wkw_lawyer in PostgreSQL and DESCRIBE wkw_lawyer in MySQL). Try doing manage.py reset wkw My guess is that the wkw_lawyer table in the database probably has a field called education_id instead of school_id, the reset command should drop and create the wkw_lawyer table again (like my predecessor proposed). More about how syncdb works (and why it wouldn't have changed education_id into school_id) here: http://docs.djangoproject.com/en/dev/ref/django-admin/#syncdb And finally, you don't have to change the name of the class when you don't like how the plural of a class name is being rendered. You can specify the plural in the Meta class of the model: http://docs.djangoproject.com/en/dev/topics/db/models/#meta-options http://docs.djangoproject.com/en/dev/ref/models/options/#verbose-name-plural Hope that helps :) On Nov 10, 3:14 am, Zeynel wrote: > Hello, > > These are my tables: > > BEGIN; > CREATE TABLE "wkw_school" ( > "id" integer NOT NULL PRIMARY KEY, > "school" varchar(200) NOT NULL > ) > ; > CREATE TABLE "wkw_lawyer" ( > "id" integer NOT NULL PRIMARY KEY, > "first" varchar(20) NOT NULL, > "initial" varchar(2) NOT NULL, > "last" varchar(20) NOT NULL, > "year_graduated" datetime NOT NULL, > "school_id" integer NOT NULL REFERENCES "wkw_school" ("id") > ) > ; > COMMIT; > > (I changed "Education" to "School" because in the admin panel it > showed up as "Educations". I think "Schools" makes more sense.) > > I created the admin and I registered School and Lawyer. > > In the admin panel School link works fine but when I click on Lawyer > link I get a "TemplateSyntaxError at /admin/wkw/lawyer/ > Caught an exception while rendering: no such column: > wkw_lawyer.school_id" > > But according to the above tables, school_id has been created. > > I tried to add in the shell, > > p = Lawyer(school_id=1) > > but > > p.save() > > throws an error. > > Any ideas why Lawyer link in the admin panel does not work? > > This is the offending line on the debug page: > > 78 {% result_list cl %} > > Thank you. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
forms.ChoiceField and empty values
Hi, I have the following problem... I've got a form, which looks like this: class CompetitionSearchForm(forms.Form): name = forms.CharField(required=False) type = forms.ChoiceField(choices=COMPETITION_TYPE_CHOICES, required=False) The tuple COMPETITION_TYPE_CHOICES is used in a model class (the purpose of the form is to search through Competition objects). It looks something a bit like this: COMPETITION_TYPE_CHOICES = ( (1, 'Olympic Games'), (2, 'ISU Championships'), (3, 'Grand Prix Series'), ) I'd like the select widget in ChoiceField to display an empty label. According to the documentation (http://docs.djangoproject.com/en/dev/ ref/forms/fields/#choicefield), it seems like this should happen by default, but I don't get an empty value in the select widget at all. Why is this? Monika --~--~-~--~~~---~--~~ 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: Advantages of using ManyToManyField(Bar, through='Foo_Bar') syntax vs not declaring a ManyToManyField at all
Thanks :) On Oct 22, 5:52 pm, Tom Evans wrote: > On Thu, 2009-10-22 at 07:57 -0700, Monika Sulik wrote: > > Hi, > > > I was wondering what exactly are the advantages of having code like > > this: > > > class Foo (models.Model): > > bar_m2m = models.ManyToManyField(Bar,through='Foo_Bar') > > > class Bar (models.Model): > > pass > > > class Foo_Bar (models.Model): > > foo = models.ForeignKey(Foo) > > bar = models.ForeignKey(Bar) > > x = models.IntegerField() > > > over having the same code, but having the Foo class like so: > > > class Foo (models.Model): > > pass > > > In both cases I can use foo.foo_bar_set and bar.foo_bar_set (assuming > > foo is a Foo Object and bar is a Bar Object). Why would I need the > > bar_m2m variable? > > I'm probably missing something pretty obvious, but if someone could > > point out the answer to me that would be great :) > > > Monika > > With a many to many with a through table, you can either go directly to > the other end of the relationship, or look at the through table. Without > it, you cannot look directly at the relationship, only at the link. > > Eg, Users can be in many UserGroups and UserGroups can have many Users. > If I use a m2m relationship with a through table, I can do both these > things: > > class UserGroup(models.Model): > users=ManyToManyField(User, through='UserUserGroupPreferences') > > class UserUserGroupPreferences(models.Model): > user=models.ForeignKey('User') > usergroup=models.ForeignKey('UserGroup') > is_group_super_user=models.BooleanField(default=False) > > u=User.objects.get(...) > # I can see directly which groups a user is in > u.usergroup_set.all() > # I can also examine things about a particular user -> group association > u.userusergrouppreferences_set.filter(is_group_super_user=True) > > Without the m2m field, I wouldn't be able to go directly to the > usergroup from the user, I would only be able to go via the through > table. > > Cheers > > Tom --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Advantages of using ManyToManyField(Bar, through='Foo_Bar') syntax vs not declaring a ManyToManyField at all
Hi, I was wondering what exactly are the advantages of having code like this: >> class Foo (models.Model): bar_m2m = models.ManyToManyField(Bar,through='Foo_Bar') class Bar (models.Model): pass class Foo_Bar (models.Model): foo = models.ForeignKey(Foo) bar = models.ForeignKey(Bar) x = models.IntegerField() >> over having the same code, but having the Foo class like so: >> class Foo (models.Model): pass >>> In both cases I can use foo.foo_bar_set and bar.foo_bar_set (assuming foo is a Foo Object and bar is a Bar Object). Why would I need the bar_m2m variable? I'm probably missing something pretty obvious, but if someone could point out the answer to me that would be great :) Monika --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---