0
我有一個視頻對象,管理員可以使用該對象。我希望能夠將這些精選視頻按其製作日期排序。在布爾字段中添加時間戳
這裏是模型我現在有 -
class VideoMedia(models.Model):
uploaded_by = models.ForeignKey('UserProfile')
video = models.ImageField(upload_to='videos')
info = models.ForeignKey('VideoInfo', blank = True, null=True)
class VideoInfo(models.Model):
title = models.CharField(max_length=256)
featured = models.BooleanField # need time also
...
我的相關視圖代碼看起來是這樣的 -
video_set = VideoInfo.objects.all()
if sort == 'featured':
videos = video_set.filter(featured=1) # .order_by('timestamp') ?
我嘗試添加一個FK的特色領域,但它使我非常難以在視圖/模板中顯示正確的數據。
class FeaturedVideos(models.Model):
video = models.ForeignKey(VideoMedia)
timestamp = models.DateTimeField(auto_now_add=True)
# in view
if sort == 'featured':
videos = FeaturedVideos.objects.order_by('timestamp')
# this doesn't work, because I need to be relative to the VideoInfo model
# for the information to display correctly in the template
什麼是最直接的方式來完成這項任務?謝謝。
正如在'features = models.DateTimeField(auto_now_add = True,null = True,blank = True)'? – David542
我不會使用auto_now_add = True,它會在添加一行時自動設置該值,這會使所有視頻具有特色。我會做我以上所做的。 null = True,並且blank = True,除非您爲特色功能傳遞值,否則它將默認爲null。 –
@Ken,好的,謝謝,我得到了db結構,它工作正常。現在在視圖中,我將如何按'非空'進行過濾,例如'if sort =='featured':videos = video_set.filter(featured = NOT NULL).order_by('featured')'? – David542