I have two models,
class Publication(models.Model):
title = models.CharField(max_length=30)
class Article(models.Model):
headline = models.CharField(max_length=100)
publications = models.ManyToManyField(Publication)
I am trying to learn serializing using serpy.
I wrote two serializers, but I am not sure how to mention the model. I wrote a django rest framework serializer, as follows,
class PublicationSerializer(serializers.ModelSerializer):
class Meta:
model = Publication
fields = 'title',
class ArticleSerializer(serializers.ModelSerializer):
publications = PublicationSerializer(many=True)
class Meta:
model = Article
fields = '__all__'
This is the serializers that I wrote for using with Serpy.
class PublicationSerializer(serpy.Serializer):
title = serpy.Field()
class ArticleSerializer(serpy.Serializer):
headline = serpy.Field()
publications = PublicationSerializer()
I dont know where should I mention the model,
I would like to be able to serialize a queryset, say
Article.objects.all()
what changes must be made to use it with Django Rest Framework?
You apparently don't need to specify an associated model for the serpy serializer. Passing your Django objects to the appropriate serpy serializer class should suffice. Or not?
articles = Article.objects.all()
articles_serialized = ArticleSerializer(articles, many=True).data
Related
I am working in django-rest-framework and I have three models: Event, Performer, and Link. I have many-to-many relationships established on the Event and Performer models as 'links' pointing to the Link model. In the API view, when I am creating or updating an event or performer, I am given a list of all links. I would like them to be removed as options once they've been associated with another object, but I can't seem to figure out how to. Below is my code:
class Link(models.Model):
created = models.DateTimeField(auto_now_add=True)
address = models.URLField()
def __str__(self):
return f"{self.address}"
class Meta:
ordering = ['created']
class Performer(models.Model):
created = models.DateTimeField(auto_now_add=True)
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
links = models.ManyToManyField(Link)
def __str__(self):
return f"{self.first_name} {self.last_name}"
class Meta:
ordering = ['created']
class Event(models.Model):
created = models.DateTimeField(auto_now_add=True)
sale_date = models.DateTimeField()
event_date = models.DateTimeField()
performer = models.ForeignKey(Performer, on_delete=models.CASCADE)
links = models.ManyToManyField(Link)
class Meta:
ordering = ['event_date']
and I'm using this for serializers:
class LinkSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Link
fields = ['url', 'address']
class PerformerSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Performer
fields = ['url', 'first_name', 'last_name', 'links']
class EventSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Event
fields = ['url', 'performer', 'sale_date', 'event_date', 'links']
I thought about using
ManyToManyField.limit_choices_to
but I don't know what my selector would look like. I also thought I could use
Link.objects.exclude(...)
or
Link.objects.filter(...)
call somewhere but I just don't know where. Thanks to anyone who can help!
Edit: thought I’d add that what I thought would work is to use ‘limit_choices_to’ to filter out any links that are included in a relationship, but I couldn’t figure out how to test if an object was in a relationship (and since there’s multiple relationships only testing for one isn’t perfect either)
You should make use of the Serializer class' get_queryset method:
class LinkSerializer(serializers.HyperlinkedModelSerializer):
def get_queryset(self):
return super().get_queryset().filter(performer=None, event=None)
class Meta:
model = Link
fields = ['url', 'address']
I figured out what I was trying to accomplish with this: I needed to restrict the choices for the field at the model level, which I was able to do by passing a predetermined restriction to the 'limit_choices_to=' parameter. See code below and thank you to #anthony2261 for the suggestion, your filter section helped me to understand how to filter even though it wasn't the type of filtering I needed!
# create a dict of filter conditions(?)
restrict_choices = {'performer': None, 'event': None}
class Performer(...):
...
# refer to the restriction defined previously
# when defining the links relationship.
links = models.ManyToManyField(Link, limit_choices_to=restrict_choices)
I'm actually create APIs on a Django Website using Django Rest Framework.
I'm trying to document them using Swagger.
I'm using Django 2.1, django-rest-swagger 2.2 and djangorestframework 3.11
Everything is nearly working as expected except something :
Let me explain you :
I have this model (models.py)
class Technology(models.Model):
"""
This model defines the different technologies
"""
name = models.CharField(max_length=CHAR_SHORT)
path = models.CharField(max_length=CHAR_SHORT, validators=[validate_tech_path], help_text='this is only used to construct the url')
image = models.ImageField()
mailer = models.EmailField(blank=True)
external = models.BooleanField(default=False)
internal = models.BooleanField(default=False)
class Meta:
verbose_name_plural = "technologies"
ordering = ['name']
def __str__(self):
return self.name
Then I have the corresponding serializer class (serializer.py):
class TechnologySerializer(serializers.ModelSerializer):
"""
This model defines the different technologies
"""
class Meta:
model = Technology
fields = ('id', 'name', 'path', 'image', 'mailer', 'external', 'internal')
Finally I have my view with generated APIs (views.py):
class TechnologyViewSet(viewsets.ModelViewSet):
queryset = Technology.objects.all()
serializer_class = TechnologySerializer
http_method_names = ['get','post','delete','put']
Here's the result :
Api description 1
Api description 2
As you see on the picture above, the parameters are une the json body.
Is it possible to have something like this for all the parameters :
API parameter wanted
Thanks a lot.
Try using ListCreateAPIView instead of modelviewset you will be able to see your post https://www.django-rest-framework.org/api-guide/generic-views/#listcreateapiview.
from rest_framework.generics import ListCreateAPIView
class TechnologyViewSet(ListCreateAPIView):
queryset = Technology.objects.all()
serializer_class = TechnologySerializer
Hope it help.
As I'm using django rest framework for building my product api.
Here is my model in models.py
class Tag(models.Model):
tag = models.CharField(max_length=10, unique=True)
class Product(models.Model):
product_name = models.CharField(max_length=100)
tag = models.ManyToManyField(Tag, blank=True, default=None, related_name='product_tag')
serializers.py :
class TagSerializer(serializers.ModelSerializer):
class Meta:
model = Tag
fields = '__all__'
class ProductSerializer(serializers.HyperlinkedModelSerializer):
tag = TagSerializer(many=True, read_only=True)
class Meta:
model = Product
fields = '__all__'
views.py :
class ProductViewSet(viewsets.ModelViewSet):
queryset = Product.objects.all()
serializer_class = ProductSerializer
I have given the url for ProductViewset, so when I hit the api it gives me the results as well but it takes too much time to load, it takes around 2 minutes to give me the response.
I'm having 2000 product objects in database which needs to be populated.
When I exclude the 'tag' field in "ProductSerializer", response comes very fast with all 2000 records.
Please suggest where is the loophole, why its affecting performance so much especially when I add this ManyToMany field.
I always use django-debug-toolbar to debug my queryset to find bottleneck/duplicate query in my project. Django orm always using lazy load to retrieve related fields from database.
You can change this default behavior of your queryset by eager load your many to many field using prefetch_related.
class ProductViewSet(viewsets.ModelViewSet):
queryset = Product.objects.prefetch_related('tag').all()
serializer_class = ProductSerializer
Reference: prefetch_related
Before posting this question I've read few questions on SOF. but they are from 2012 and very confusing as well.
for e.g Django Rest Framework - Get related model field in serializer
my question is very straight forward
models.py
class User(models.Model):
username = models.CharField(max_length=100,unique=True)
password = models.CharField(max_length=100,null=False,blank=False)
class Car(models.Model):
user = models.ForeignKey(User)
car_name = models.CharField(max_length=100,null=True,blank=True)
serializers.py
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('username','password' )
class CarSerializer(serializers.ModelSerializer):
#user = ?? what should I write or is there any better approach for serializing Car objects
class Meta:
model = Car
fields = ('user','car_name')
views.py
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
class CarViewSet(viewsets.ModelViewSet):
queryset = Car.objects.all()
serializer_class = CarSerializer
Please suggest all possible approaches.
1 more query. which one is better ModelSerializer or HyperlinkModelSerializer. as I saw different different answers containing these two.
You just need to do:
class CarSerializer(serializers.ModelSerializer):
user = UserSerializer()
class Meta:
model = Car
fields = ('user','car_name')
that is all.
Also, you should take a look to the kindnesses of serializers.Field and serializers.SerializerMethodField, you can play with them and customize your response data as much as you wish.
As to the question around HyperlinkedModelSerializer-ModelSerializer, very clear here:
The HyperlinkedModelSerializer has the following differences from ModelSerializer:
It does not include the pk field by default.
It includes a url field, using HyperlinkedIdentityField.
Relationships use HyperlinkedRelatedField, instead of PrimaryKeyRelatedField.
Hope that helps.
I have a Django model that is hierarchical using django-mptt, which looks like:
class UOMCategory(MPTTModel, BaseModel):
"""
This represents categories of different unit of measurements.
"""
name = models.CharField(max_length=50, unique=True)
description = models.CharField(max_length=50, unique=True)
parent = TreeForeignKey('self', null=True, blank=True, related_name='%(app_label)s_%(class)s_sub_uom_categories')
The problem now is I created a REST API using Django REST Framework; how do I make sure that parent field returns serialized data?
Here is the Model Serializer:
class UOMCategorySerializer(BaseModelSerializer):
"""
REST API Serializer for UOMCategory model
"""
class Meta:
model = UOMCategory
In DRF you can use a serializer as a field in another serializer. However, recursion is not possible.
Tom Christie posted a solution on another question (Django rest framework nested self-referential objects). His solution will also work with your problem.
In your UOMCategorySerializer.Meta class you specify the fields you want to use, also list the parent and/or children field(s) there. Then you use Tom Christies solution.
In your case this would give:
class UOMCategorySerializer(ModelSerializer):
class Meta:
model = UOMCategory
fields = ('name', 'description', 'parent', 'children')
Tom Christies solution: By specifying what field to use for parent and/or children, you avoid using too much (and possibily endless) recursion:
UOMCategorySerializer.base_fields['parent'] = UOMCategorySerializer()
UOMCategorySerializer.base_fields['children'] = UOMCategorySerializer(many=True)
The above works for me in a similar situation.
Simple DRF API view using MPTT cache and DRF serializer.
from rest_framework import serializers
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from events.models import Category
class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = Category
fields = (
"name",
"slug",
)
class CategoryTreeView(GenericAPIView):
serializer_class = CategorySerializer
def get(self, request, *args, **kwargs):
root_nodes = Category.objects.all().get_cached_trees()
data = []
for n in root_nodes:
data.append(self.recursive_node_to_dict(n))
return Response(data)
def recursive_node_to_dict(self, node):
result = self.get_serializer(instance=node).data
children = [self.recursive_node_to_dict(c) for c in node.get_children()]
if children:
result["children"] = children
return result