无论如何,我们可以在哪里使用django rest框架构建逻辑,用户可以相应地添加具有多个图像和内容的博客,并且当保存和检索时,应该能够显示相同类型的UI,依赖于前端应用程序,就像媒体平台一样
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发布于 2020-08-06 05:16:09
阅读Django中的关系(以及一般的SQL )
这听起来像是你在寻找类似以下的东西:
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对象,那么按特定顺序获取数据应该很容易:
使用像order_by这样的东西将帮助您以您喜欢的顺序返回响应。
我在这里编写的模型并不是实现您设定的目标的唯一方法,我强烈建议您阅读Django中的关系和模型。
https://stackoverflow.com/questions/63254815
复制相似问题