Django 元管理排序 - 排除语法文章

发布于 2024-12-24 20:33:54 字数 710 浏览 2 评论 0原文

我目前有这样的模型代码块:

class Book(models.Model):
    title = models.CharField(max_length=100)
    num_pages = models.IntegerField(blank=True, null=True)
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher)
    publication_date = models.DateField()

    def __unicode__(self):
        return self.title

    class Meta:
        ordering = ["title"]

如果我有一本名为“The Fry Chronicles”或“A Hitchhikers Guide to the Galaxy”(例如)的书名,则为有没有办法让 Django 忽略管理界面中单词之前的那些短文章(an、a、the 等),而只按文章后面的单词对结果进行排序?

更进一步,也许有一种方法可以像这样排列结果......

  • 弗莱编年史,
  • 银河系漫游指南,A

至于将文章移到书名的末尾吗?

有人可以提出解决上述两个问题的方案吗?

提前致谢!

I currently have this chunk of model code:

class Book(models.Model):
    title = models.CharField(max_length=100)
    num_pages = models.IntegerField(blank=True, null=True)
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher)
    publication_date = models.DateField()

    def __unicode__(self):
        return self.title

    class Meta:
        ordering = ["title"]

If I have a book title called 'The Fry Chronicles' or 'A Hitchhikers Guide to the Galaxy' (for example), is there a way to get Django to ignore those short articles (an, a, the etc.) before the word within the admin interface, and just order the results by the words that follows the article?

To take it a step further, maybe there is a way to lay out the results like this...

  • Fry Chronicles, The
  • Hitchhikers Guide to the Galaxy, A

As to move the article to the end of the book title?

Could anybody propose a solution to the above two issues?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏末的微笑 2024-12-31 20:33:54

您可以尝试,

class Book(models.Model):
    displayed_title = models.CharField(max_length=100)
    ordered_title = models.CharField(max_length=100)

    def __unicode__(self):
        return self.displayed_title

    class Meta:
        ordering = ["ordered_title"]

并且只需确保在存储之前解析 ordered_title 即可。

You could try,

class Book(models.Model):
    displayed_title = models.CharField(max_length=100)
    ordered_title = models.CharField(max_length=100)

    def __unicode__(self):
        return self.displayed_title

    class Meta:
        ordering = ["ordered_title"]

And just make sure that you are parsing ordered_title before you store it.

最近可好 2024-12-31 20:33:54

我认为如果不破解内部 django 管理代码,这是不可能的。

不过,如果冗余不是问题,您可以采取一些小技巧。 Book 中有额外的字段,表示订购的标题(如 clean_title)和使用 ordering 管理中的属性

I don't think this is possible without hacking internal django admin code.

You can however do a little trick if some redundancy is not a problem. Have additional field in Book that represents title for ordering (like clean_title) and the use ordering attribute in admin.

酒绊 2024-12-31 20:33:54

不知道这是否有效...未经测试...只是一个想法,请记住 django 只是 python...我希望这有助于让您走上正轨!?!

def splitter(title):  
    list_title=[]
    a = split(title)    
        for word in a:
            if len(word) > 3: # With this you filter "a" "the" etc. but not "Bob" etc.!!
               word.append(list_title)
    return list_tile


class Meta:
    ordering = splitter(title) # not sure if this is working....

Don't know if this is working....untested...just an idea to keep in mind that django is just python...I hope that helps to get you to the right track!?!

def splitter(title):  
    list_title=[]
    a = split(title)    
        for word in a:
            if len(word) > 3: # With this you filter "a" "the" etc. but not "Bob" etc.!!
               word.append(list_title)
    return list_tile


class Meta:
    ordering = splitter(title) # not sure if this is working....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文