Answer the question
In order to leave comments, you need to log in
How to bind multiple submodels to one model in Django?
Hello! I am making a single page application on Django 1.9 (with rest framework). The essence of the problem is this:
There are only two models Case (Cases) and Article (Many articles about each case), on the main page only a list of previews for Cases is displayed. But on the page with a detailed description of a particular case, in addition to information from the Case model itself, you need to display several Article instances of this case, and here questions arise. To then pass all the data to the appropriate template, I use a serializor:
from rest_framework import serializers
from landing.models import Case, Article
class CasePreviewSerializer(serializers.ModelSerializer):
class Meta:
model = Case
fields = [
'id',
'name',
'preview',
'slider_preview',
'url',
]
class CaseDetailSerializer(serializers.ModelSerializer):
class Meta:
model = Case
fields = [
'name',
'slider_preview',
]
class Meta:
model = Article
fields = [
'picture',
'caption',
]
from django.db import models
from django.utils.translation import ugettext_lazy as _
class Case(models.Model):
name = models.CharField(_('case name'), max_length=30)
preview = models.ImageField(_('preview image'), upload_to="images")
created_at = models.DateTimeField(_('created at'), auto_now_add=True)
slider = models.BooleanField(_('is slider preview availible'))
if slider:
slider_preview = models.ImageField(_('slider preview'), upload_to="images")
def __str__(self):
return self.name
class Article(models.Model):
case = models.ForeignKey(Case)
picture = models.ImageField(_('picture'), upload_to="images")
caption = models.TextField(_('text'), max_length=501)
def __str__(self):
return self.caption
from rest_framework import viewsets
from .serializers import *
class CaseViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Case.objects.all()
def get_serializer_class(self):
if self.action == 'list':
return CasePreviewSerializer
return CaseDetailSerializer
Answer the question
In order to leave comments, you need to log in
The Case and Article models must be associated with a ForeignKey.
class Article(models.Model):
...
case = models.ForeignKey(Case, related_name='articles')
...
class CaseSerializer(serializers.ModelSerializer):
class Meta:
model = Case
fields = (..., ..., 'articles')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question