English 中文(简体)
Django:比较其他领域
原标题:Django: comparison on extra fields

www.un.org/Depts/DGACM/index_spanish.htm 简短问题: Django是否有办法,根据某些领域的字母顺序,以不敏感的方式找到下一行?

www.un.org/Depts/DGACM/index_french.htm 我在数据库中有几句话,并对它们有详细的看法。 我谨按字母顺序逐字逐句。 因此,我需要按字母顺序列出前言和下字。 现在我做的是:

class Word(models.Model):
    original = models.CharField(max_length=50)
    ...

    def neighbours(self):
        """
        Returns the words adjacent to a given word, in alphabetical order
        """
        previous_words = Word.objects.filter(
            original__lt=self.original).order_by( -original )
        next_words = Word.objects.filter(
            original__gt=self.original).order_by( original )
        previous = previous_words[0] if len(previous_words) else None
        next = next_words[0] if len(next_words) else None
        return previous, next

问题是,这确实是一种对案件敏感的比较,例如<代码>。 Foo见bar,这不是我想要的。 为了避免这一问题,在另一个观点中——在我列举所有话时,我已经利用了增加额外领域的习惯模式主管。

class CaseInsensitiveManager(models.Manager):

    def get_query_set(self):
        """
        Also adds an extra  lower  field which is useful for ordering
        """
        return super(CaseInsensitiveManager, self).get_query_set().extra(
            select={ lower :  lower(original) })

and in the definition of Word I add

objects = models.Manager()
alpha = CaseInsensitiveManager()

这样,我就能够提出问题。

Word.alpha.all().order_by( lower )

并按字母顺序读取所有字,不论情况如何。 但Icannot do

class Word(models.Model):
    original = models.CharField(max_length=50)
    ...

    objects = models.Manager()
    alpha = CaseInsensitiveManager()

    def neighbours(self):
        previous_words = Word.objects.filter(
            lower__lt=self.lower()).order_by( -lower )
        next_words = Word.objects.filter(
            lower__gt=self.lower()).order_by( lower )
        previous = previous_words[0] if len(previous_words) else None
        next = next_words[0] if len(next_words) else None
        return previous, next

确实,Django将不接受fieldviewupsextrafield。 因此,我应该做些什么(没有书写KQ习俗)?

www.un.org/Depts/DGACM/index_spanish.htm Bonus questions: 我在做些什么时至少会看到更多的问题。 首先,我不相信业绩。 我假定,在I下定义previous_wordsnext_words时,根本不发生任何疑问。 当我界定<代码>previous和next时,数据库中唯一一个查询方式将发生,产生一个或多个疑问。

SELECT Word.original, ..., lower(Word.original) AS lower
WHERE lower < `foo`
ORDER BY lower DESC
LIMIT 1

这项权利吗? 或者,我做些什么会使数据库过于缓慢? 我不知道有关Django ORM内部工作的足够细节。

The second problem is that I actually have to cope with words in different languages. Given that I know the language for each word, is there a way to get them in alphabetical order even if they have non-ASCII characters. For instance I d want to have méchant, moche in this order, but I get moche, méchant.

最佳回答

数据库应当能够为你进行这种分类,它应当能够在没有“下级”功能的情况下这样做。

真正需要的是数据库的核对和编码。

例如,如果你使用我的手法,你可以使用定型号8和 coll字号8。

如果这种核对工作对你不利,你可以根据你们的需要和数据库进行其他核对。 但是,在盘问中利用外事和职能,是一个令人费解的工作,将减缓 app。

在我sql和海报中也有许多可供选择的:

http://dev.mysql.com/doc/refman/5.5/en/charset-mysql.html http://stackoverflow.com/questions/1423378/postgresql-utf8-character-comparison

但是,这无疑是一个好的机会,可以选择在 d一级。

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签