1

I have a model for articles which takes a field FileField which is supposed to be a markdown file for the user to load their article already. I expose the api with a ModelViewSet.

This is saved to my media folder. I could fetch the content from the client side by GETing it from the href of course but that would mean 2 requests to my server:

  1. get article info (title, content- this is the md, date published, description, etc.. ).
  2. get content from the link.

But i'm wondering if there's a way to tell django to just send the content of the file instead of the href when it responds to a requestion for the article item.

Here's my model and api:

# ---------------------- # # src/articles/models.py # # ---------------------- # from os.path import splitext from uuid import uuid4 from django.db import models # Create your models here. def hashFilename(instance, name): ext = splitext(name)[1] return "articles/{}{}".format(uuid4(), ext) def hashImageFilename(instance, name): ext = splitext(name)[1] return "images/{}{}".format(uuid4(), ext) class Article(models.Model): title = models.CharField(("title"), max_length=100) content = models.FileField("content", upload_to=hashFilename) description = models.TextField(("description"), default='') uploadDate = models.DateTimeField(("uploadDate"), auto_now=True) lastModified = models.DateTimeField(("uploadDate"), auto_now=True) publicationDate = models.DateField("publicationDate") image = models.ImageField("image", upload_to=hashImageFilename) def __str__(self): return self.title 
# ------------------------- # # src/articles/api/views.py # # ------------------------- # from rest_framework.viewsets import ModelViewSet from ..models import Article from .serializers import ArticleSerializerFull, ArticleSerializerShort class ArticlesViewSet(ModelViewSet): queryset = Article.objects.all() def get_serializer_class(self): if self.action == 'list': serializer = ArticleSerializerShort else: serializer = ArticleSerializerFull return serializer queryset = Article.objects.all() 

1 Answer 1

3
+50

Defining a serializers.SerializerMethodField--(DRF Doc) method will do the job.

class ArticleSerializer(serializers.ModelSerializer): content = serializers.SerializerMethodField() def get_content(self, article): return article.content.file.read() class Meta: fields = '__all__' model = Article

Alternatively, you could achieve the same by overriding the to_representation method of the serializer.

class ArticleSerializer(serializers.ModelSerializer): class Meta: fields = '__all__' model = Article def to_representation(self, instance): rep = super().to_representation(instance) rep['content'] = instance.content.file.read() return rep

Update-1

From this comment, I hope you need a live markdown editor in Django Admin rather than a FileField.

So, Use any of these markdown packages to get a live view in the Django Admin. These packages are using models.TextField to store the markdown content. So that you could read the content from the field in anywhere just like any other model fields

Sign up to request clarification or add additional context in comments.

7 Comments

This is great. The only problem is that it doesn't leave the admin with an editing UI after having uploaded the file.
Sorry, I don't understand your concern. Can you explain a little bit more?
I'll break down the use-case: My user is an admin on a website with his writings. 1. He prepares an article on his own text-editor in MD format. 2. He goes to the admin page, creates a new article, and for content uploads the MD file. 3. The front-end fetches the content well, and displays it directly on the site (after your suggested correction). 4. The user noticed an issue, he wants to update the article - currently he has to go to the file on his machine, update it, and upload it. It would be nicer if he could just edit it in the admin panel. (I understand that might be a stretch).
AFAIK, Since this content is associated with a FileField, you can't edit with a live view.
Alright the packages are indeed what i was looking for! Thanks!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.