模型管理排序?

发布于 2024-11-02 21:05:37 字数 1198 浏览 0 评论 0原文

使用 Django 管理界面,如何确保 HTML select multiple 中的对象按某种顺序排序(最好按字母顺序)?问题是我有 3 个模型 - CD、歌曲、歌手。在 CD 管理仪表板中,歌曲是内联到 CD 的,而歌手是一个我想排序的多字段!

我的 model.py 文件:

class CD(models.Model):

    cd_name = models.CharField("CD Name",max_length=50)
    date = models.DateField("CD Release Date")
    photo = models.ImageField("CD Cover",blank=True,upload_to='covers')
    singers = models.ManyToManyField(Singer,blank=True,null=True) 

    def __unicode__(self):
        return self.cd_name

class Song(models.Model):

    cid = models.ForeignKey(CD)
    track_num = models.PositiveIntegerField("Track Number",max_length=2) 
    song_name = models.CharField("Song Name",max_length=50)
    soloists = models.ManyToManyField(Singer,blank=True,null=True) 
    stream_url = models.URLField("Stream URL", blank=True)

    def __unicode__(self):
        return self.song_name

class Singer(models.Model): (not relevent)

我的 admin.py 文件:

class SongInline(admin.TabularInline):
    model = Song
    extra = 0

class CDAdmin(admin.ModelAdmin):

    list_display = ('cd_name', 'date')

    inlines = [
        SongInline,
    ]

admin.site.register(CD, CDAdmin)

With the Django Admin interface, how do you ensure that objects within HTML select multiple are sorted in some order (prefer alphabetical)? The issue is that I have 3 models - CD, Song, Singer. One the CD admin dashboard, Song is inline to CD and Singer is a manytomany field that I would like sorted!

My model.py file:

class CD(models.Model):

    cd_name = models.CharField("CD Name",max_length=50)
    date = models.DateField("CD Release Date")
    photo = models.ImageField("CD Cover",blank=True,upload_to='covers')
    singers = models.ManyToManyField(Singer,blank=True,null=True) 

    def __unicode__(self):
        return self.cd_name

class Song(models.Model):

    cid = models.ForeignKey(CD)
    track_num = models.PositiveIntegerField("Track Number",max_length=2) 
    song_name = models.CharField("Song Name",max_length=50)
    soloists = models.ManyToManyField(Singer,blank=True,null=True) 
    stream_url = models.URLField("Stream URL", blank=True)

    def __unicode__(self):
        return self.song_name

class Singer(models.Model): (not relevent)

My admin.py file:

class SongInline(admin.TabularInline):
    model = Song
    extra = 0

class CDAdmin(admin.ModelAdmin):

    list_display = ('cd_name', 'date')

    inlines = [
        SongInline,
    ]

admin.site.register(CD, CDAdmin)

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

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

发布评论

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

评论(1

尛丟丟 2024-11-09 21:05:37

formfield_for_manytomany

class SongInline(admin.TabularInline):
    model = Song
    extra = 0

    def formfield_for_manytomany(self, db_field, request, **kwargs):
            if db_field.name == "soloists":
                kwargs["queryset"] = Singer.objects.order_by('last_name')
            return super(SongInline, self).formfield_for_manytomany(db_field, request, **kwargs)

答案您的“ModelAdmin Ordering”具体问题,但在您的情况下,您可以通过模型简单地为您的 m2m 模型定义默认顺序ordering 模型元类选项。

http://docs.djangoproject.com/en/dev/ref /模型/选项/#ordering

class Singer(models.Model):
    # my model
    class Meta:
        ordering = ['name'] # your select box will respect this as well.

formfield_for_manytomany

class SongInline(admin.TabularInline):
    model = Song
    extra = 0

    def formfield_for_manytomany(self, db_field, request, **kwargs):
            if db_field.name == "soloists":
                kwargs["queryset"] = Singer.objects.order_by('last_name')
            return super(SongInline, self).formfield_for_manytomany(db_field, request, **kwargs)

That answers your specific question of "ModelAdmin Ordering" but in your case, you can simply define a default ordering for your m2m model via the model ordering model meta class option.

http://docs.djangoproject.com/en/dev/ref/models/options/#ordering

class Singer(models.Model):
    # my model
    class Meta:
        ordering = ['name'] # your select box will respect this as well.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文