2

请看看我的模型。

class BackgroundImage(models.Model):
    user = models.ForeignKey(User)
    image = models.ImageField(upload_to=get_upload_file_name)
    caption = models.CharField(max_length=200)
    pub_date = models.DateTimeField(default=datetime.now)

class ProfilePicture(models.Model):
    user = models.ForeignKey(User)
    image = models.ImageField(upload_to=get_upload_file_name)
    caption = models.CharField(max_length=200)
    pub_date = models.DateTimeField(default=datetime.now)

class Album(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=200)
    pub_date = models.DateTimeField(default=datetime.now)

    class Meta:
        ordering = ['-pub_date']
        verbose_name_plural = ('Albums')

    def __unicode__(self):
        return self.name

class Photo(models.Model):
    user = models.ForeignKey(User)
    album = models.ForeignKey(Album, default=3)
    image = models.ImageField(upload_to=get_upload_file_name)
    caption = models.CharField(max_length=200)
    pub_date = models.DateTimeField(default=datetime.now)

我如何从一组中获取所有图像PhotoProfilePicture以及BackgroundImage它们的图像image field。然后过滤它们-pub_date以显示在模板中?请帮帮我。将不胜感激!谢谢你。

编辑

注意:我需要ProfilePictureBackgroundImage使用UserProfile这样的东西:

from django.db import models
from django.contrib.auth.models import User
from profile_picture.models import ProfilePicture
from background_image.models import BackgroundImage

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    permanent_address = models.TextField()
    temporary_address = models.TextField()
    profile_pic = models.ForeignKey(ProfilePicture)
    background_pic = models.ForeignKey(BackgroundImage)

    def __unicode__(self):
        return self.user.username

User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
4

1 回答 1

4

有一个InheritanceManager提供作为其中的一部分django-model-utils允许您执行此操作,请参阅docs

在 Linux / Mac 中安装:

sudo pip install django-model-utils

烦人的是,使用easy_installpip在 Windows 上安装并不那么简单,请参阅:如何在 Windows 上安装 Python 包?. 一个快速而肮脏的方法是django-model-util/这里下载目录到你的 django 项目的顶层目录,如果你打算复制整个项目以部署到生产网络服务器,这很方便。

为了使用InheritanceManager,模型需要稍微重构:

from django.db import models
from django.contrib.auth.models import User
from datetime import datetime
from model_utils.managers import InheritanceManager

get_upload_file_name = 'images/' # I added this to debug models

class BaseImage(models.Model):
    user = models.ForeignKey(User)
    image = models.ImageField(upload_to=get_upload_file_name)
    caption = models.CharField(max_length=200)
    pub_date = models.DateTimeField(default=datetime.now)

    objects = InheritanceManager()

class BackgroundImage(BaseImage):
    pass

class ProfilePicture(BaseImage):
    pass

class Album(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=200)
    pub_date = models.DateTimeField(default=datetime.now)

    class Meta:
        ordering = ['-pub_date']
        verbose_name_plural = ('Albums')

    def __unicode__(self):
        return self.name

class Photo(BaseImage):
    album = models.ForeignKey(Album, default=3)

所有的 Image 模型现在都继承自一个通用的超类,它创建了InheritanceManager. 我还将所有重复的属性上移到超类中,但这并不是绝对必要的,使用InheritanceManager意味着BaseImage仍然可以在模板中访问任何不存在的属性。

要检索按 排序的列表-pubdate

BaseImage.objects.select_subclasses().order_by("-pub_date")

在视图中使用:

def recentImages(request):
    r = BaseImage.objects.select_subclasses().order_by("-pub_date")[:20]
    return  render_to_response("recentImages.html", { "imageList" : r })

在模板中使用:

{% for photo in imageList %}
  <img src="{{ photo.image.url }}" />
{% endfor %}

这是否像您正在寻找的东西?

编辑

对于新模型,以下代码仍然可以正常工作:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    permanent_address = models.TextField()
    temporary_address = models.TextField()
    profile_pic = models.ForeignKey(ProfilePicture)
    background_pic = models.ForeignKey(BackgroundImage)

只要确保ForeignKey关系中最后两个模型的名称是正确的!

于 2013-11-10T13:27:13.947 回答