I'm using Django Rest Framework to build an API where I have the following models of Users making, confirming and showing interest on Events:
models.py
class User(AbstractBaseUser, PermissionsMixin):
user_name = models.CharField(_("user name"), max_length=150, unique=True)
slug = AutoSlugField(populate_from='user_name', unique=True)
class Event(models.Model):
name = models.CharField(max_length=100, blank=False, null=False)
owner = models.ForeignKey(User, related_name="owned_events", on_delete=models.SET_NULL, blank=True, null=True)
confirmed = models.ManyToManyField(User, related_name="confirmed_events", blank=True)
interested = models.ManyToManyField(User, related_name="interested_events", blank=True)
to serialize it I used the following code as I found here and at the DRF docs:
serializers.py
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = [
"url",
"user_name",
"password",
]
extra_kwargs = { "password": {"write_only": True} }
class EventSerializer(serializers.HyperlinkedModelSerializer):
owner = UserSerializer(required=False)
confirmed = UserSerializer(required=False, many=True)
interested = UserSerializer(required=False, many=True)
class Meta:
model = Event
lookup_field = 'slug'
extra_kwargs = { 'url': {'lookup_field': 'slug'} }
fields = [
"url",
"owner",
"name",
"confirmed",
"interested",
]
It works just fine like that, but I wanted the UserSerializer to show confirmed and interested events of each user just like each event shows the users confirmed and interested. I changed serializers and got the url of each event, like that:
serializers.py with HyperlinkedRelatedField on UserSerializer
class UserSerializer(serializers.HyperlinkedModelSerializer):
confirmed_events = serializers.HyperlinkedRelatedField(
queryset=Event.objects.all(),
view_name='event-detail',
lookup_field='slug',
many=True,
required=False
)
interested_events = serializers.HyperlinkedRelatedField(
queryset=Event.objects.all(),
view_name='event-detail',
lookup_field='slug',
many=True,
required=False
)
class Meta:
model = User
fields = [
"url",
"user_name",
"password",
"confirmed_events",
"interested_events",
]
extra_kwargs = { "password": {"write_only": True} }
This got me the following JSON from the User model:
{
"user_name": "d",
"confirmed_events": [],
"interested_events": [
"http://localhost:8000/events/eqwer-2/",
"http://localhost:8000/events/test/",
"http://localhost:8000/events/test-2/",
"http://localhost:8000/events/test-3/",
]
}
And from the Event model:
{
"url": "http://localhost:8000/events/eqwer-2/",
"owner": null,
"name": "eqwer",
"slug": "eqwer-2",
"confirmed": [],
"interested": [
{
"user_name": "d",
"confirmed_events": [],
"interested_events": [
"http://localhost:8000/events/eqwer-2/",
"http://localhost:8000/events/test/",
"http://localhost:8000/events/test-2/",
"http://localhost:8000/events/test-3/",
]
}
]
},
The Event model JSON is fine because it shows each user's data, but I wanted the User JSON to show each event data instead of just the event URL, it'd be something like:
{
"user_name": "d",
"confirmed_events": [],
"interested_events": [
{
"url": "http://localhost:8000/events/eqwer-2/",
"owner": null,
"name": "eqwer",
"slug": "eqwer-2",
},
]
}
Create a separate serializer for interested_events like so:
class InterestedEventsSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
fields = ('url', 'owner', 'name', 'slug')
And in your UserSerializer declare the interested_events using the serializer above:
class UserSerializer(serializers.HyperlinkedModelSerializer):
confirmed_events = ... #
interested_events = InterestedEventsSerializer(many=True)
class Meta:
model = User
fields = [
"url",
"user_name",
"password",
"confirmed_events",
"interested_events",
]
extra_kwargs = { "password": {"write_only": True} }
Related
I have 2 models for admin and member positions and I would like to get both of the models in one API call to fetch the data on my front end. How can I achieve this?
class ClinetAdminPosition(models.Model):
name = models.CharField(max_length=128, null=True)
company = models.ForeignKey(
to="Company", on_delete=models.CASCADE, related_name="admin_positions", null=True
)
modified_at = models.DateTimeField(verbose_name="Updated", auto_now=True, editable=True)
created_at = models.DateTimeField(verbose_name="Created", auto_now_add=True, editable=False)
def __str__(self):
return f"{self.name}"
class ClinetMangerPosition(models.Model):
name = models.CharField(max_length=128, null=True)
company = models.ForeignKey(
to="Company", on_delete=models.CASCADE, related_name="manger_positions", null=True
)
modified_at = models.DateTimeField(verbose_name="Updated", auto_now=True, editable=True)
created_at = models.DateTimeField(verbose_name="Created", auto_now_add=True, editable=False)
def __str__(self):
return f"{self.name}"
I want to get both models' data from 1 API request
to be like this:
[
{
"admin_positions": [
{
"name": "test",
"company": 1
},
{
"name": "test2",
"company": 1
},
{
"name": "test3",
"company": 1
}
],
"manger_position": [
{
"name": "test",
"company": 1
},
{
"name": "test2",
"company": 1
},
{
"name": "test3",
"company": 1
}
]
}
]
You can do something like this.
**This is your serializers classes **
class ClinetMangerPositionSerializer(ModelSerializer)
clas Meta:
model = ClinetMangerPosition
fields = ['name','company']
class ClinetAdminPositionSerializer(ModelSerializer):
class Meta:
model = ClinetAdminPosition
fields = ['name','company']
This will be your views class to display data.
from rest_framework.views import APIView
from . import serializers
from rest_framework.response import Response
class ViewName(APIView):
def get(self, request):
admin = ClinetAdminPosition.objects.all()
manager = ClinetMangerPosition.objects.all()
res :{
"admin_positions": serializers.ClinetAdminPositionSerializer(admin, many=True).data
"manager_positions":serializers.ClinetMangerPositionSerializer(manager, many-True).data
}
return Response(res)
something like this,
Let me know if you got any doubt
class ClinetMangerPositionSerializer(ModelSerializer)
clas Meta:
model = ClinetMangerPosition
fields = ['name','company']
class ClinetAdminPositionSerializer(ModelSerializer):
class Meta:
model = ClinetAdminPosition
fields = ['name','company']
class CompanySerializer(MOdelSerializer):
manger_position = ClinetAdminPositionSerializer(many=True,source
='clinetmangerposition')
admin_position = ClinetAdminPositionSerializer(many=True,source
='clinetadminposition')
class Meta:
model= Company
fields = ['manger_position','admin_position']
I have the following models:
class Platform(models.Model): # Get
key = models.CharField(max_length=32, unique=True)
class PlatformVersion(models.Model): # Get or create
platform = models.ForeignKey(Platform, on_delete=models.CASCADE)
major = models.IntegerField(db_index=True)
minor = models.IntegerField(db_index=True)
build = models.IntegerField(db_index=True)
class DeviceManufacturer(models.Model): # Get or create
name = models.CharField(max_length=64, unique=True)
class DeviceModel(models.Model): # Get or create
manufacturer = models.ForeignKey(DeviceManufacturer, on_delete=models.CASCADE)
platform = models.ForeignKey(Platform, on_delete=models.CASCADE)
# ...
# And many others
POST JSON looks like this:
{
"device_model": {
"manufacturer": {
"name": "samsung"
},
"platform": {
"key": "android"
}
},
"version": {
"platform": {
"key": "android"
},
"major": 8,
"minor": 1,
"build": 0
}
}
I have the following serializers:
class PlatformSerializer(serializers.ModelSerializer):
class Meta:
model = Platform
fields = ('name', 'key',)
extra_kwargs = {
'name': {
'read_only': True,
},
}
def create(self, validated_data):
return Platform.objects.get(key=validated_data['key'])
class PlatformVersionSerializer(serializers.ModelSerializer):
platform = PlatformSerializer()
class Meta:
model = PlatformVersion
fields = ('platform', 'major', 'minor', 'build',)
def create(self, validated_data):
# ?
pass
class DeviceManufacturerSerializer(serializers.ModelSerializer):
class Meta:
model = DeviceManufacturer
fields = ('name',)
def create(self, validated_data):
manufacturer, _ = DeviceManufacturer.objects.get_or_create(
defaults={
'name': validated_data['name'],
},
name=validated_data['name']
)
return manufacturer
class DeviceModelSerializer(serializers.ModelSerializer):
manufacturer = DeviceManufacturerSerializer()
platform = PlatformSerializer()
class Meta:
model = DeviceModel
fields = ('manufacturer', 'platform')
def create(self, validated_data):
# ?
pass
I've marked in serializers places where I don't know how to correctly do the object creation, as some of nested objects are «read-only» (I mean that I in fact use get, not create), some of them are get or create.
How should I do it?
Is there any way to display the name of the user in the "likedBy" section of the view, instead of the user id? Using django rest framework
From view I get , ignore comments:
[
{
"id": 3,
"title": "ddsa",
"content": "dsadsa",
"created": "2021-02-10T08:07:42.758400Z",
"updated": "2021-02-10T08:07:42.758400Z",
"author": 1,
"category": [
{
"pk": 1,
"name": "Life"
}
],
"likedBy": [
1
],
"comments": [
{
"id": 2,
"content": "ghfa",
"created": "2021-02-10T08:08:02.407950Z",
"post": 3,
"author": 1,
"likedBy": [
1
]
}
]
}
]
Views.py:
class PostViewSet(FlexFieldsMixin, generics.ListAPIView):
queryset = Post.objects.all()
serializer_class = PostSerializer
permit_list_expands = ['category', 'comments']
Models.py
class Post(models.Model):
title = models.CharField(max_length=255)
content = models.TextField()
category = models.ManyToManyField(Category, related_name='posts')
author = models.ForeignKey(User, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
likedBy = models.ManyToManyField(User, related_name='posts', blank=True)
class Meta:
ordering = ['-created']
Serializers.py:
class PostSerializer(FlexFieldsModelSerializer):
class Meta:
model = Post
fields = '__all__'
expandable_fields = {
'category': ('blogApi.CategorySerializer', {'many': True}),
'comments': ('blogApi.CommentSerializer', {'many': True}),
}
How serialize ManyToMany field to display text values
Given that you are not serializing a relation, but rather an attribute of your model which is related to your user, I believe you have to use a serializer.SerializerMethodField(). This allows you to do the following:
class PostSerializer(FlexFieldsModelSerializer):
liked_by = serializers.SerializerMethodField(method_name="get_user_likes")
class Meta:
model = Post
fields = (
"title",
"content",
"category",
"author",
"created",
"update",
"liked_by"
)
expandable_fields = {
'category': ('blogApi.CategorySerializer', {'many': True}),
'comments': ('blogApi.CommentSerializer', {'many': True}),
}
#classmethod
def get_user_likes(obj):
# do whatever you like here, but I tend to call a
# method on my model to keep my serializer file
# nice and tidy
# you'll need to define a UserSerializer
return UserSerializer(obj.get_user_likes(), many=True, read_only=True)
And in your Post model:
class Post(models.Model):
title = models.CharField(max_length=255)
content = models.TextField()
category = models.ManyToManyField(Category, related_name='posts')
author = models.ForeignKey(User, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
likedBy = models.ManyToManyField(User, related_name='posts', blank=True)
class Meta:
ordering = ['-created']
def get_user_likes(self):
return self.likedBy.all()
You can of course define the full method in your serializer, but as I said I like to keep the serializer as clean as possible and put all methods associated with models in my models.py.
#classmethod
def get_user_likes(obj):
# you'll need to define a UserSerializer
return UserSerializer(obj.likedBy.all(), many=True, read_only=True)
So you can set
likedBy = serializers.ReadOnlyField(source='get_likedBy')
in your PostSerializer class
and define function in your Post model class like below:
#property
def get_likedBy(self):
liked_by = []
for user in self.users_liked_post.all():
liked_by.append(user.name)
return liked_by
just use correct related_name instead of users_liked_post
If you add likedBy to your expandable fields, and then add ?expand=likedBy to your url, it should give you all the information that you outline in the UserSerializer, or write a new serializer named LikedBySerializer. Also as a general rule, try not to use '__all__' it's a good way to leak data. Happy coding!
class LikedBySerializer(FlexFieldsModelSerializer):
class Meta:
model = User
fields = '__all__'
class PostSerializer(FlexFieldsModelSerializer):
class Meta:
model = Post
fields = '__all__'
expandable_fields = {
'category': ('blogApi.CategorySerializer', {'many': True}),
'comments': ('blogApi.CommentSerializer', {'many': True}),
'likedBy': ('blogApi.LikedBySerializer', {'many': True}),
}
I am trying to get all the bills and their customer-related details (i.e. 'customer_code', 'email' etc.) with it.
However, source='user.customer_code does not seem to have any effect at all. What am I missing?
I have been following along this:
this stackoverflow post with no luck.
My two models:
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(db_index=True, unique=True, max_length=200)
customer_code = models.CharField(max_length=300, blank=True, null=True, default=None)
class Bill(models.Model):
customer = models.ForeignKey(
User, on_delete=models.CASCADE, blank=True, null=True, related_name="customer_bill"
)
payable_amount = models.DecimalField(max_digits=10, decimal_places=2, default=0)
View:
class BillView(APIView):
def get(self, request, format=None):
q = Bill.objects.all().select_related('customer')
s = BillSerializer(q, many=True)
return JsonResponse({
"bill": s.data
})
Serializer:
class BillSerializer(serializers.ModelSerializer):
customer_code = serializers.CharField(source='user.customer_code', read_only=True)
class Meta:
model = Bill
fields = ('id','payable_amount','customer_code') # binding customer_code here
Current Output:
"bill": [
{
"id": 1,
"payable_amount": "1000.00"
},
{
"id": 2,
"payable_amount": "2000.00"
}
]
Expected Result:
"bill": [
{
"id": 1,
"payable_amount": "1000.00",
"customer_code": "CUS10001" # want this to be attached
},
{
"id": 2,
"payable_amount": "2000.00",
"customer_code": "CUS10002" # want this to be attached
}
]
I am trying to nest my Users table inside my Relationships table. So instead of this:
[
{
"user": 1,
"related_user": 2,
"relationship": "followed_by"
}
]
I am trying to get this:
[
{
"user": {
"username": "user1",
"name": "User 1",
"email": "bla",
"phone": "bla",
"date_joined": "2017-11-01T21:34:13.101256Z"
},
"related_user": {
"username": "user2",
"name": "User 2",
"email": "bla",
"phone": "bla",
"date_joined": "2017-11-01T21:34:13.101256Z"
},
"relationship": "followed_by"
}
]
I looked up tutorials and I tried adding serializers.RelatedField , UserSerializer(many=true, read-only=true) etc. but nothing worked
Models.py
class User(models.Model):
username = models.CharField(max_length=255)
name = models.CharField(max_length=255)
email = models.CharField(max_length=255)
phone = models.CharField(max_length=255)
date_joined = models.DateTimeField(auto_now_add=True, blank=True)
def __str__(self):
return str(self.pk) + ", " + self.username
RELATIONSHIP_CHOICE = [
("follows", "follows"),
("followed_by", "followed_by"),
("none", "none"),
]
class Relationship(models.Model):
user = models.ForeignKey(User, related_name="primary_user", null=True)
related_user = models.ForeignKey(User, related_name="related_user", null=True)
relationship = models.CharField(max_length=40, choices=RELATIONSHIP_CHOICE, default=RELATIONSHIP_CHOICE[0])
Serializers.py
from rest_framework import serializers
from . import models
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = models.User
fields = (
'username',
'name',
'email',
'phone',
'date_joined',
)
class RelationshipSerializer(serializers.ModelSerializer):
related_user = UserSerializer(many=True)
class Meta:
model = models.Relationship
fields = (
'user',
'related_user',
'relationship',
'related_user'
)
I tried to add related user to my serializer but it didnt work. I am getting an error: 'User' object is not iterable
Any help is appreciated.
class RelationshipSerializer(serializers.ModelSerializer):
user = UserSerializer(read_only=True)
related_user = UserSerializer(read_only=True)
class Meta:
model = models.Relationship
fields = (
'user',
'related_user',
'relationship'
)
user = UserSerializer(read_only=True, many=True) is for manytomany field,user = UserSerializer(read_only=True) is for ForeignKey field.