【发布时间】:2019-01-18 22:43:58
【问题描述】:
我有一个在 models.py 中定义的对象播放列表:
class Playlist(models.Model):
"""Allow a user to create a customized list of songs."""
name = models.CharField(max_length=100)
image = models.ImageField(upload_to='playlists/%Y/%m/%d', blank=True, null=True)
songs = models.ManyToManyField('Song')
description = models.TextField(blank=True, null=True, max_length=1000)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
"""String for representing the model object."""
return self.name
def get_absolute_url(self):
"""Returns the url to access a detail record for this song."""
return reverse('playlist-detail', args=[str(self.id)])
我也有继承自该模型的 AddNewPlaylist 表单:
class AddPlaylistForm(forms.ModelForm):
class Meta:
model = Playlist
fields = ['name', 'image', 'description', 'songs']
我只希望表单中的“歌曲”对象按其出现在表单中的字母顺序排列 - 我该怎么做?
编辑:我不想更改数据库中 song 模型的顺序 - 只是它在 AddPlayList 表单中的排序方式。
【问题讨论】:
标签: django-models django-forms django-2.1