0

Is there anyway where we can build logic Using django rest framework where user can add blog with multiple images and content accordingly and when saved and retrieved it should be able to display the same kind of UI depening up on the frontend app same like medium platform

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
John
  • 137
  • 2
  • 12

1 Answers1

0

Read about relationships in Django (and SQL in general)

django relations

it sounds like you're looking for something like the following:

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)

Some notes:

I haven't dealt myself with Images field But I remember it does require a special library (pillow).

getting the data in a certain order should be easy enough if you understand the queryset object:

queryset link

using stuff like order_by will help you return the response in the order you like.

the models I've written here are not the only way to achieve the goal you've set, I strongly recommend reading about relations and models in Django.