2 Questions!

9 views
Skip to first unread message

Sebastien Armand [Pink]

unread,
Jan 30, 2007, 7:56:12 AM1/30/07
to django...@googlegroups.com
I've been trying Django and have 2 small problems:

- When I have a foreign key field in a model, is it possible to use just the id of the foreign object or do we always have to use the object itself?
Example with a school and a student class. Each student has a foreign key linking to the school where he studies. Through a form, I get the informations to create the student including the school's id. When I want to create and save the student, am I obliged to first hit the database to get the school object and then pass it to student(school=school_object) or is there a way to just use the ID as it is?


- When I search something in my database, using icontains on a field, it doesn't work when there are non-asccii characters: a product field may contain (in french so with accents!!) télévision but when I look for "télé" I get a unicode error. If I look for "vision" everything works just fine. Any idea why?

Thanks

vfo...@gmail.com

unread,
Jan 30, 2007, 9:12:43 AM1/30/07
to Django users
1. No, you can create a student with: Student.objects.create(name="Foo
Bar", school_id=2)

2. Not sure... I have a Django application here, and when I search for
"è", it returns the restaurant "Les Deux Frères" without problems.
Could be an encoding problem with your database or with your terminal
or with Python. That's the problem with encoding issues: they take a
lot of time to figure out and properly fix.

Hope this helps.

Vincent.

On Jan 30, 7:56 am, "Sebastien Armand [Pink]" <khe...@gmail.com>
wrote:

Sebastien Armand [Pink]

unread,
Jan 30, 2007, 9:30:34 AM1/30/07
to django...@googlegroups.com
It helps!

I'll check better for the encoding problems...

James Tauber

unread,
Jan 30, 2007, 9:40:52 AM1/30/07
to django...@googlegroups.com

On Tue, 30 Jan 2007 13:56:12 +0100, "Sebastien Armand [Pink]"
<khe...@gmail.com> said:
> - When I search something in my database, using icontains on a field, it
> doesn't work when there are non-asccii characters: a product field may
> contain (in french so with accents!!) télévision but when I look for
> "télé"
> I get a unicode error. If I look for "vision" everything works just fine.
> Any idea why?

Sounds like one is a Unicode object and the other an encoded string
(maybe UTF-8 or Latin-1).

James
--
James Tauber http://jtauber.com/
journeyman of some http://jtauber.com/blog/

Sebastien Armand [Pink]

unread,
Jan 30, 2007, 9:51:12 AM1/30/07
to django...@googlegroups.com
As you talk about this, if I remember well, the database is UTF-8 but the web page which contains the form has no encoding declared. May this be the source of troubles?

2007/1/30, James Tauber < jta...@jtauber.com>:

Michael Radziej

unread,
Jan 30, 2007, 10:03:40 AM1/30/07
to django...@googlegroups.com
Sebastien Armand [Pink]:

> - When I search something in my database, using icontains on a field, it
> doesn't work when there are non-asccii characters: a product field may
> contain (in french so with accents!!) télévision but when I look for "télé"
> I get a unicode error. If I look for "vision" everything works just fine.
> Any idea why?

What did you do exactly to search, and what is the exact backtrace?

--
noris network AG - Deutschherrnstraße 15-19 - D-90429 Nürnberg -
Tel +49-911-9352-0 - Fax +49-911-9352-100

http://www.noris.de - The IT-Outsourcing Company

Kenneth Gonsalves

unread,
Jan 30, 2007, 9:07:05 PM1/30/07
to django...@googlegroups.com

On 30-Jan-07, at 6:26 PM, Sebastien Armand [Pink] wrote:

> Example with a school and a student class. Each student has a
> foreign key linking to the school where he studies. Through a form,
> I get the informations to create the student including the school's
> id. When I want to create and save the student, am I obliged to
> first hit the database to get the school object and then pass it to
> student(school=school_object) or is there a way to just use the ID
> as it is?

in the database django stores this as school_id - and when you save
you can use that by specificaly setting school_id on the save instead
of school where django will automatically get and insert the id

--

regards
kg
http://lawgon.livejournal.com
http://nrcfosshelpline.in/web/


Sebastien Armand [Pink]

unread,
Jan 31, 2007, 2:56:17 PM1/31/07
to django...@googlegroups.com
So the exact unicode error is:

UnicodeEncodeError at /entreprise/search/

'ascii' codec can't encode character u'\xe9' in position 1: ordinal not in range(128)

Request Method: POST
Request URL: http://localhost/entreprise/search/
Exception Type: UnicodeEncodeError
Exception Value: 'ascii' codec can't encode character u'\xe9' in position 1: ordinal not in range(128)
Exception Location: c:\Python25\lib\site-packages\django\db\models\fields\__init__.py in , line 25


with the following traceback:

Traceback (most recent call last):
File "c:\Python25\lib\site-packages\django\template\__init__.py" in render_node
  712. result = node.render(context)
File "c:\Python25\lib\site-packages\django\template\defaulttags.py" in render
  100. len_values = len(values)
File "c:\Python25\lib\site-packages\django\db\models\query.py" in __len__
  100. return len(self._get_data())
File "c:\Python25\lib\site-packages\django\db\models\query.py" in _get_data
  430. self._result_cache = list(self.iterator())
File "c:\Python25\lib\site-packages\django\db\models\query.py" in iterator
  171. select, sql, params = self._get_sql_clause()
File "c:\Python25\lib\site-packages\django\db\models\query.py" in _get_sql_clause
  444. joins2, where2, params2 = self._filters.get_sql(opts)
File "c:\Python25\lib\site-packages\django\db\models\query.py" in get_sql
  574. joins2, where2, params2 = val.get_sql(opts)
File "c:\Python25\lib\site-packages\django\db\models\query.py" in get_sql
  622. return parse_lookup(self.kwargs.items(), opts)
File "c:\Python25\lib\site-packages\django\db\models\query.py" in parse_lookup
  743. joins2, where2, params2 = lookup_inner(path, lookup_type, value, opts, opts.db_table, None)
File "c:\Python25\lib\site-packages\django\db\models\query.py" in lookup_inner
  915. params.extend(field.get_db_prep_lookup(lookup_type, value))
File "c:\Python25\lib\site-packages\django\db\models\fields\__init__.py" in get_db_prep_lookup
  172. return ["%%%s%%" % prep_for_like_query(value)]
File "c:\Python25\lib\site-packages\django\db\models\fields\__init__.py" in
  25. prep_for_like_query = lambda x: str(x).replace("\\", "\\\\").replace("%", "\%").replace("_", "\_")

  UnicodeEncodeError at /entreprise/search/
  'ascii' codec can't encode character u'\xe9' in position 1: ordinal not in range(128)

___________________________________________
And finally, here's my search view:

def search_entreprise(request):
    form = EntrepriseSearchForm()
    if request.method=='POST':
        new_data = request.POST.copy()
        form = EntrepriseSearchForm(new_data)
        if form.is_valid():
            data = form.clean_data
            entreprise_list = Entreprise.objects.all()
            if data['domaine']:
                entreprise_list=entreprise_list.filter(domaine=data['domaine'])
            if data['entrepriseMere']:
                entreprise_list=entreprise_list.filter(entrepriseMere=data['entrepriseMere'])
            if data['nom']:
                entreprise_list=entreprise_list.filter(nom__icontains=data['nom'])
            return render_to_response('stagesECL/entreprise_list.html',{'list':entreprise_list,})
    return render_to_response('stagesECL/entreprise_search.html',{'form':form,})


where in this case the only information given is the entreprise name (nom) which had the value "hé".

I tried to change the encoding in the html but this doedn't seem to help....
Hope somebody knows how to do this!

2007/1/31, Kenneth Gonsalves <law...@thenilgiris.com>:

Sebastien Armand [Pink]

unread,
Jan 31, 2007, 2:57:16 PM1/31/07
to django...@googlegroups.com
Wow, sorry, I copied paste and didn't see the font was so awful!

2007/1/31, Sebastien Armand [Pink] <khe...@gmail.com>:

Michael Radziej

unread,
Feb 1, 2007, 1:39:47 AM2/1/07
to django...@googlegroups.com
Hi Sebastien,

I don't see where the exact problem is and whether there's already a
ticket, but this is a bug within the newforms package. It is caused
due to newforms putting unicode into the models, but most parts of
Django are not unicode ready. You can search in the existing tickets
(with category == 'newforms') whether there's already a patch.

If not, could you please open a new ticket and provide exactly the
information from the mail I'm replying to? If you put {{{ and }}}
around the traceback and the code, it won't look as bad as in email
(it's always like this in mail). And please put a link to this
thread's google archive, if you can.

Sebastien Armand [Pink]:

the str(x) throws this exception when x is not an ASCII character.

>
> UnicodeEncodeError at /entreprise/search/
> 'ascii' codec can't encode character u'\xe9' in position 1: ordinal not in
> range(128)

Michael

Sebastien Armand [Pink]

unread,
Feb 1, 2007, 4:11:16 AM2/1/07
to django...@googlegroups.com
No problem, thanks!

There's already a ticket, it's here:
http://code.djangoproject.com/ticket/3387

But though, is there another encoding I could use and how do I specify it?

2007/2/1, Michael Radziej <m...@noris.de>:

Michael Radziej

unread,
Feb 1, 2007, 4:30:23 AM2/1/07
to django...@googlegroups.com
Sebastien Armand [Pink]:

> No problem, thanks!
>
> There's already a ticket, it's here:
> http://code.djangoproject.com/ticket/3387

No, thanks for searching, but it's not. #3387 was triggered by using

queryset.filter(unicode_string)

You didn't use a unicode_string within filter(), though the
traceback is similar. It happened because Django's ORM internal code
looks up the related records to a foreign key with filter(), and
uses a unicode string because because newforms put it in the model.

It's a new bug, but the patch in the #3387 should heal it. Funny.
Please *do* open a ticket. #3387 is closed. I'm now pretty sure that
your case has not been reported before.

> But though, is there another encoding I could use and how do I specify it?

Short answer: No, unless you want to patch Django or python ;-)

The concept of a default encoding is doomed, and python has finally
learned the lesson. I guess that you can use unicode(bytestring)
without giving an encoding at all is a trap that stems from times
when python had a settable default encoding. ASCII is used as a
default encoding in these circumstances. The same applies for
str(unicode_string) or for (bytestring == unicode_string). All these
are traps, traps, traps.

There is a hook in site.py, but this was left in for experiments. If
you use it, you'll probably get into devil's kitchen.

Sebastien Armand [Pink]

unread,
Feb 1, 2007, 7:19:25 AM2/1/07
to django...@googlegroups.com
Oooops didn't read enaough from the #3387 ticket. So I'll open one!
Thanks a lot for your explanations.

2007/2/1, Michael Radziej <m...@noris.de >:

Sebastien Armand [Pink]

unread,
Feb 1, 2007, 7:32:44 AM2/1/07
to django...@googlegroups.com
This time it's done. The ticket is here: http://code.djangoproject.com/ticket/3412 I hope it's enough documented.

2007/2/1, Sebastien Armand [Pink] < khe...@gmail.com>:

Michael Radziej

unread,
Feb 1, 2007, 8:15:42 AM2/1/07
to django...@googlegroups.com
Sebastien Armand [Pink]:

> This time it's done. The ticket is here:
> http://code.djangoproject.com/ticket/3412 I hope it's enough documented.

Merci! I finally realized that it's not about a ForeignKey, sorry
for the confusion. Can you check if you can work around it with a

entreprise_list=entreprise_list.filter(domaine=data['domaine'].encode(settings.DEFAULT_CHARSET))

etc.?

Hope that helps!

Sebastien Armand [Pink]

unread,
Feb 1, 2007, 1:05:26 PM2/1/07
to django...@googlegroups.com
The patch provided for ticket 3387 works perfectly!

2007/2/1, Michael Radziej <m...@noris.de>:
Reply all
Reply to author
Forward
0 new messages