如何使 Django ImageField 将图像保存到另一个模型?
我有一个成员配置文件应用程序,用于存储有关团队成员的简单信息。 配置文件的一部分是用作头像的图像。 我一直在使用 Photologue 创建标准照片库,但它有一个ImageModel,可以扩展以利用 Photologue 的大小调整和缓存功能。
问题是,他们在页面上给出的示例如下所示:
from django.contrib.auth.models import User
from photologue.models import ImageModel
class UserPortrait(ImageModel):
user = models.OneToOneField(User, primary_key=True)
我可以将 User 替换为 Member,一切都会好起来的,除了编辑个人资料变成了一个两步过程; 首先,我必须创建一个会员个人资料,然后创建一个 UserPortrait 并将其与会员关联。 我想将其简化为一个步骤过程,就像我使用 ImageField 一样。
我已经在表单级别和 admin.py 级别覆盖了图像字段,但我想知道是否可以在模型级别甚至字段级别处理这个问题。 我希望拥有 ImageField 的内联功能,但仍然让文件由单独的相关模型管理。 实际模型内联似乎有点矫枉过正,因为它只是一张图像。
I have a member profiles application that stores simple information about members of a team. Part of the profile is an image to be used as an avatar. I've been using Photologue to create standard galleries of photos, but it has an ImageModel that can be extended to take advantage of Photologue's resizing and caching functionality.
The problem is, the example they give on their page looks like this:
from django.contrib.auth.models import User
from photologue.models import ImageModel
class UserPortrait(ImageModel):
user = models.OneToOneField(User, primary_key=True)
I could replace User with Member and all would be good, except for the fact that editing the profile becomes a two step process; First I'd have to create a Member profile, then create a UserPortrait and associate it with the Member. I'd like to streamline this back into a single step process, as if I were using an ImageField.
I've overridden image fields at the form level and the admin.py level, but I'm wondering if I can deal with this at the model level or even the field level. I'd like to have the inline functionality of an ImageField but still have the file managed by a separated, related model. Actual model inlines seem like overkill since its just one image.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 Member 类应该扩展 ImageModel。 不需要额外的类(例如UserPortrait)。
Your Member class should extend ImageModel. There is no need for an additional class (e.g. UserPortrait).