模型管理排序?
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
formfield_for_manytomany
答案您的“ModelAdmin Ordering”具体问题,但在您的情况下,您可以通过模型简单地为您的 m2m 模型定义默认顺序
ordering
模型元类选项。http://docs.djangoproject.com/en/dev/ref /模型/选项/#ordering
formfield_for_manytomany
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