首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django博客使用Rest框架将多个图像连接到单个对象中内容

Django博客使用Rest框架将多个图像连接到单个对象中内容
EN

Stack Overflow用户
提问于 2020-08-05 04:47:18
回答 1查看 21关注 0票数 0

无论如何,我们可以在哪里使用django rest框架构建逻辑,用户可以相应地添加具有多个图像和内容的博客,并且当保存和检索时,应该能够显示相同类型的UI,依赖于前端应用程序,就像媒体平台一样

代码语言:javascript
复制
Note:
My question isn't about adding multiple images and content using Rest framework 
but its about fetching and displaying the data based on how user sent it the server
For eg:
<Image>
content for that image
<Image2>
content for this image
i just want to know how to associate those images to that content 
i want to add content to that image 
or is there anyway where we can store image and all the content exacty and save it in TextField 
I've searched a lot about this but unfortunately I've not found a way to make this happen
EN

回答 1

Stack Overflow用户

发布于 2020-08-06 05:16:09

阅读Django中的关系(以及一般的SQL )

django relations

这听起来像是你在寻找类似以下的东西:

代码语言:javascript
复制
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):
    # Always override the user model provided by Django when starting a project. the docs themselves state that.
    pass


class Image(models.Model):
    image = models.ImageField()
    added = models.DateTimeField(auto_now_add=True)
    # using get_user_model to get the User model, always better then referencing User directly
    user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, related_name="user_images",
                             null=False,
                             blank=False
                             )


class ImageContent(models.Model):
    title = models.CharField(max_length=140, null=False, blank=False)
    content = models.TextField(max_length=500)
    image = models.OneToOneField(Image, on_delete=models.CASCADE, null=False, blank=False)

一些注意事项:

我还没有处理过图像字段,但我记得它确实需要一个特殊的库(枕头)。

如果您理解queryset对象,那么按特定顺序获取数据应该很容易:

queryset link

使用像order_by这样的东西将帮助您以您喜欢的顺序返回响应。

我在这里编写的模型并不是实现您设定的目标的唯一方法,我强烈建议您阅读Django中的关系和模型。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63254815

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档