How to nest models fields under another key for serializer - python

I have this type of Post model
class Post(models.model):
id = models.UUIDField(primary_key=True, default=uuid4)
user = models.ForeignKey(UserProfile, null=True, on_delete=models.CASCADE)
tags = models.ManyToManyField("Tags", blank=True, related_name="posts")
title = models.CharField(max_length=200)
body = RichTextField(blank=True, null=True)
post_views = models.IntegerField(default=0)
created_at = models.DateTimeField(auto_now_add=True, verbose_name=("created_at"))
in the serializer, I want to nest title, body, created_at under another key named article
serializers.py
class ArticleSerializer(serializers.ModelSerializer):
title = serializers.CharField()
content = serializers.CharField(source="body")
created_at = serializers.CharField()
class Meta:
model = Post
fields = ("title", "body", "created_at")
class PostSerializer(serializers.ModelSerializer):
user = serializers.SlugRelatedField(slug_field="email", read_only=True)
article = ArticleSerializer(required=True)
tags = TagSerializer(many=True, required=False)
post_views = serializers.IntegerField(read_only=True)
def to_representation(self, instance):
data = super().to_representation(instance)
cat = data.pop("category")
title = data.pop("title")
content = data.pop("body")
created_at = data.pop("created_at")
data["categories"] = cat
data["article"] = {"title": title, "content": content, "created_at": created_at}
return data
class Meta:
model = Post
fields = "__all__"
views.py
class ArticleView(APIView):
def get_object(self, pk: int):
try:
if pk:
return Post.objects.filter(pk=pk, draft=False)
return Post.objects.filter(draft=False)
except Post.DoesNotExist:
raise Http404
def get(self, request: Request, pk=None):
post = self.get_object(pk)
serializer = PostSerializer(post, many=True)
return Response(serializer.data)
The error that I'm getting
AttributeError: Got AttributeError when attempting to get a value for field `article` on
serializer `PostSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the
`Post` instance.
Original exception text was: 'Post' object has no attribute 'article'

fields = ["article","modelfield", etc etc]
in post serializer instead of all, call the fields

Related

Serialize a queryset including the ForeignKey values

models.py:
class Project(models.Model):
project_code = models.CharField(max_length=250, null=False, blank=False)
description = models.CharField(max_length=1000, null=True, blank=True)
class ProjectManager(models.Model):
name = models.CharField(max_length=250, null=False, blank=False)
project_id = models.ForeignKey(
Project,
on_delete=models.CASCADE
)
views.py:
def ajax_search(request):
if request.method == 'GET':
category = request.GET['category']
if category == 'Project':
result = Project.objects.all()
data = serialize("json", result, cls=DatetimeJSONEncoder)
return HttpResponse(data, content_type="application/json")
else:
result = ProjectManager.objects.select_related('project_id').all()
data = serialize("json", result, cls=DatetimeJSONEncoder)
return HttpResponse(data, content_type="application/json")
return HttpResponse('')
return HttpResponse('')
I would like to return a json response with the content of ProjectManager + the content of the Project associated to that ProjectManager (ForeignKey).
According to what I have read in the Django documentation, to do a left join, the best thing to do would be to use select_related.
In fact, it works, but when I serialize ProjectManager, the Project values are not included in the JSON string. How could I put all into the JSON?
you can create serializers.py file and insert this code there
# import your models
from rest_framework import serializers
class ProjectSerializer(serializers.ModelSerializer):
class Meta:
model = Project
fields = "__all__"
class ProjectManagerSerializer(serializers.ModelSerializer):
project_id = ProjectSerializer(many=True)
class Meta:
model = ProjectManager
fields = "__all__"
# views.py
qs = ProjectManager.objects.select_related('project_id').all()
resp = ProjectManagerSerializer(qs, many=True).data

I want to use ForeignKey Serializer

I have these Models Below
class Campaign(models.Model):
title = models.CharField(max_length=120)
img = models.ImageField(upload_to='products/')
def __str__(self):
return self.title
class Product(models.Model):
title = models.CharField(max_length=100)
data = models.DateField(auto_now_add=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
image = models.ImageField(upload_to="products/")
campaign = models.ForeignKey(Campaign, on_delete=models.DO_NOTHING, null=True, blank=True, related_name="products")
offer = models.ForeignKey(Offer, on_delete=models.DO_NOTHING, null=True, blank=True)
market_price = models.PositiveIntegerField()
selling_price = models.PositiveIntegerField()
description = models.TextField()
def __str__(self):
return self.title
I want to show campaign_wise products. But i cant find any solution for this. Though i tried reading the docs of DRF
Here is my Serializer
class CampaignProductsSerializer(serializers.ModelSerializer):
products = serializers.PrimaryKeyRelatedField(read_only=True, many=True)
class Meta:
model = Campaign
fields = ['title', 'products']
Whenever i try to run this i get 'Product' object has no attribute 'products'
Here is my URL
path('campaign_products/<int:id>/', CampaignProducts.as_view()),
Here is my View:
class CampaignProducts(APIView):
def get(self, request, id):
campaigns = Campaign.objects.all()
query = Product.objects.filter(campaign_id = id)
serializer = CampaignProductsSerializer(query, many=True)
return Response(serializer.data)
In that case you should make two serializers, one for the Product and one for the Campaign, so:
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = ['title'] # etc.
class CampaignProductsSerializer(serializers.ModelSerializer):
products = ProductSerializer(read_only=True, many=True)
class Meta:
model = Campaign
fields = ['title', 'products']
In the view, you then serializer a Campaign:
from django.shortcuts import get_object_or_404
# returns the title of the campaign, and the products
class CampaignProducts(APIView):
def get(self, request, id):
campaign = get_object_or_404(Campaign, id=id)
serializer = CampaignProductsSerializer(campaign)
return Response(serializer.data)
In case you do not want to include the data of the campaign, we can work with the ProductSerializer:
# returns only the related products
class CampaignProducts(APIView):
def get(self, request, id):
products = Product.objects.filter(campaign_id=id)
serializer = ProductSerializer(products, many=True)
return Response({'data': serializer.data})

How to access atribute of instance I want to validate from inside serializer?

In my view I do following:
class ReviewViewSet(viewsets.ModelViewSet):
#queryset, serializer_class and permission_classes defined here
def perform_create(self, serializer):
title_id = self.kwargs.get('title_id')
title = get_object_or_404(Title, pk=title_id)
serializer.save(author=self.request.user, title=title)
I want to validate that review doesn't exist yet. I'm trying to do this in serializer's validate():
class ReviewSerializer(serializers.ModelSerializer):
title = serializers.SlugRelatedField(slug_field='pk', read_only='True')
author = serializers.SlugRelatedField(slug_field='username', read_only='True')
def validate(self, data):
title = # <-- How to get title here?
author = self.context['request'].user
queryset = Review.objects.all().filter(title=title, author=author)
if queryset.exists():
raise serializers.ValidationError('Review alredy exists')
return(data)
class Meta:
fields = '__all__'
model = Review
Attempt to do title = self.title raises AttributeError: 'ReviewSerializer' object has no attribute 'title'
How to access title from inside validate() in my case?
Here's my Review model:
class Review(models.Model):
class Meta:
ordering = ['-pub_date']
unique_together = ['title', 'author']
title = models.ForeignKey(
Title,
on_delete=models.CASCADE,
related_name='reviews',
)
author = models.ForeignKey(
CustomUser,
on_delete=models.CASCADE,
related_name='reviews',
)
text = models.TextField('Review text')
score = models.PositiveSmallIntegerField(
'Review score',
validators=[
MinValueValidator(1),
MaxValueValidator(10)
]
)
pub_date = models.DateTimeField(
'Date and time of review',
auto_now_add=True,
db_index=True
)
The response to your question lies in the documentation : https://www.django-rest-framework.org/api-guide/serializers/#object-level-validation
About the validate method:
This method takes a single argument, which is a dictionary of field values
If you look at the code sample in the doc, you'll see that all data that you might need to validate are in the data argument of the validate method.
Hence, the title is in data['title'].

AttributeError at /apiv2/api/processorder/order/

I am trying to post multiple data into my DataBase Using Django Rest framework (DRF).
AttributeError at /apiv2/api/processorder/order/
Got AttributeError when attempting to get a value for field subcategory on serializer MyProcessOrderSerializer.
The serializer field might be named incorrectly and not match any attribute or key on the list instance.
Original exception text was: 'list' object has no attribute 'subcategory'.
models.py
class SubCategory(models.Model):
category = models.ForeignKey(Category, related_name='subcategory', on_delete=models.CASCADE)
name = models.CharField("Food Name", max_length=50, help_text="Name of The Food")
price = models.DecimalField("Food Price", max_digits=5, decimal_places=2)
quantity = models.PositiveIntegerField("Qty.", help_text="Quantity of the food Item you want")
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return f'{self.name}'
class Meta:
verbose_name = 'SubCategory'
verbose_name_plural = 'SubCategories'
class Order(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
bike = models.ForeignKey(User, related_name='bike', on_delete=models.CASCADE, blank=True, null=True)
package = models.ForeignKey(PackageType, related_name='package', on_delete=models.CASCADE, blank=True, null=True)
total_price = models.DecimalField(max_digits=10, decimal_places=2, default=0000.0)
qty = models.PositiveIntegerField(default=1)
shipping_address = models.CharField("Delivery Address", max_length=150)
paid = models.BooleanField(default=False)
ordernote = models.TextField("Order Notes", null=True)
shipped = models.BooleanField(default=False)
complete = models.BooleanField(default=False)
received = models.BooleanField(default=False)
refund_requested = models.BooleanField(default=False)
refund_granted = models.BooleanField(default=False)
ref_code = models.CharField(max_length=20, blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ('-created_at',)
def __str__(self):
return '{}'.format(self.id)
def order(self):
if not hasattr(self, '_order'):
self._order = self.order.all()
return self._order
'''
def get_total_cost(self):
total_cost = sum(orders.get_cost() for orders in self.order.all())
return total_cost
'''
class ProcessOrder(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
order = models.ForeignKey(Order, related_name='order', on_delete=models.CASCADE)
quantity = models.PositiveIntegerField("Qty.", default=1, help_text="Quantity of the food Item you want")
#category = models.ForeignKey(Category, related_name='category', on_delete=models.CASCADE)
subcategory = models.ForeignKey(SubCategory, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return f'{self.order} -- {self.subcategory.name}'
serializers.py
class MyProcessOrderSerializer(serializers.ModelSerializer):
#subcategory_name = serializers.RelatedField(source='subcategory.id', read_only=True)
#subcategory_set = SubCategoryOrderSerializer(many=True)
class Meta:
model = ProcessOrder
fields = ('quantity', 'subcategory', 'user')
read_only_fields = ('user', )
def create(self, validated_data):
return ProcessOrder.objects.create(**validated_data)
view.pf
#api_view(['POST'])
#permission_classes([IsAuthenticated])
def processorder_view(request):
orderuser = User.objects.get(id=request.user.id)
serializer = MyProcessOrderSerializer(data=request.data, many=True)
if serializer.is_valid():
order = Order.objects.create(user=orderuser, ref_code=create_ref_code())
order.save()
processorder = serializer.save(order=order, user=orderuser)
return Response(MyProcessOrderSerializer(processorder).data, status=status.HTTP_201_CREATED)
else: #return Response("Process Order Created Successfully")
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
I am now trying to make a POST such as this but getting the above error.
[{
"quantity": 16,
"subcategory": 1
},
{
"quantity": 14,
"subcategory": 3
}
]
You have to explicitly specify the relation in the serializer either with one of the built-in serializer:
Serializer relations
class MyProcessOrderSerializer(serializers.ModelSerializer):
# StringRelatedField and PrimaryKeyRelatedField are some of the built in ones.
subcategory = serializers.StringRelatedField(source='subcategory.id', read_only=True)
user = serializer.PrimaryKeyRelatedField()
class Meta:
model = ProcessOrder
fields = ('quantity', 'subcategory', 'user')
read_only_fields = ('user', )
def create(self, validated_data):
# If you want to accect new subcategory object via the endpoint you need further action here, see the docs.
return ProcessOrder.objects.create(**validated_data)
or by creating a custom subcategory serializer and then in serializer:
class UserSerializer(serializers.ModelSerializer):
class Meta:
fields = ('id', 'email', 'password', 'first_name', 'last_name')
class SubCategorySerializer(serializers.ModelSerializer):
class Meta:
fields = # whatever fields youd'e like to include
class MyProcessOrderSerializer(serializers.ModelSerializer):
# StringRelatedField is One of the built in ones.
sybcategory = SubCategorySerializer()
user = UserSerializer()
class Meta:
model = ProcessOrder
fields = ('quantity', 'subcategory', 'user')
read_only_fields = ('user', )
def create(self, validated_data):
# If you want to accect new subcategory object via the endpoint you need further action here, see the docs.
return ProcessOrder.objects.create(**validated_data)
And, I noticed you have Foreign Key to User in process order and order itself,
You could delete the user field from the process order and access it via the order relation, for that you can specify the user field in the subcategory serializer and the subcategory serializer in the process order serializer as I showed above.
And if I understand the purpose of creating a process order model, the relation should be a One to One and not Foreign key, But maybe I'm not seeing the whole picture here.
I think the problem apear in request.data in this line :
serializer = MyProcessOrderSerializer(data=request.data, many=True)
You should loop throught your object list and pass just the object not the whole list .
exemple :
{ "list_":[{"quantity": 7 ,"subcategory": 3}, {"quantity": 7 ,"subcategory": 3}] }
To get the first object from the request :
request.data.get('list_')[0]
I was able to solve the problem with this code both in my views and serialize
views.py
#api_view(['POST'])
#permission_classes([IsAuthenticated])
def processorder_view(request):
orderuser = User.objects.get(id=request.user.id)
data = request.data
order = Order.objects.create(user=orderuser, ref_code=create_ref_code())
if isinstance(data, list):
serializer = MyProcessOrderSerializer(data=request.data, many=True)
else:
serializer = MyProcessOrderSerializer(data=request.data)
if serializer.is_valid():
processorder = serializer.save(order=order, user=request.user)
return Response(status=status.HTTP_201_CREATED)
#return HttpResponse("Question created", status=201)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
serializers.py
class MyProcessOrderSerializer(serializers.ModelSerializer):
#subcategory_name = serializers.RelatedField(source='subcategory.id', read_only=True)
#subcategory_set = SubCategoryOrderSerializer(many=True)
#subcategory = serializers.StringRelatedField(source='subcategory.id', read_only=True)
class Meta:
model = ProcessOrder
fields = ('quantity', 'subcategory', 'user')
read_only_fields = ('user', )
def create(self, validated_data):
return ProcessOrder.objects.create(**validated_data)
Then, I was able to send multiple data with this sample.
[
{
"subcategory": 1,
"quantity": 12
},
{
"subcategory": 3,
"quantity": 12
}
]

Django DRF serializer on PrimaryKeyRelatedField

Question about PrimaryKeyRelatedField serialization in Django DRF version 3.4.7.
models
class UserModel(AbstractEmailUser):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return str(self.email)
class Conversation(models.Model):
admin = models.ForeignKey('UserModel', db_index=True, related_name='admin_user')
patient = models.ForeignKey('UserModel', db_index=True)
subject = models.CharField(max_length=255, db_index=True)
user_reply = models.BooleanField(default=False)
admin_seen = models.BooleanField(default=False)
expire = models.DateTimeField()
conversation_type = models.ForeignKey('ConversationType', db_index=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return str(self.admin)
class ConversationMessages(models.Model):
text = models.TextField(db_index=True)
conversation = models.ForeignKey('Conversation', db_index=True, related_name='msg_conv')
user = models.ForeignKey('UserModel', db_index=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return str(self.user)
class ConversationFiles(models.Model):
message = models.ForeignKey('ConversationMessages', db_index=True, related_name='message')
file = models.FileField(upload_to='conversations', db_index=True)
def __str__(self):
return str(self.user)
Every model has related field for Rest Framework.
Logic is create conversation, then take ID from conversation and save message model.
serialize's
class MessagesSerializer(serializers.ModelSerializer):
text = serializers.CharField(required=False)
conversation = serializers.PrimaryKeyRelatedField(queryset=Conversation.objects.all(), required=False)
user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all(), required=False)
class Meta:
model = ConversationMessages
class ConversationSerializer(serializers.ModelSerializer):
admin = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all(), required=False)
msg_conv = MessagesSerializer()
class Meta:
model = Conversation
def create(self, validated_data):
msg_conv = validated_data.pop('msg_conv', None)
admin_user = Conversation.objects.create(**validated_data)
ConversationMessages.objects.create(conversation_id=Conversation.objects.get(id=admin_user.id).id, **msg_conv)
return admin_user
Serializer is problem on POST method. Everything works great POST object create data in database, but problem is when serializer save object i get this message: 'RelatedManager' object has no attribute 'conversation'.
View
class ConversationView(APIView):
authentication_classes = (JSONWebTokenAuthentication,)
#parser_classes((FormParser, MultiPartParser, FileUploadParser))
def post(self, request):
admin = request.user.id
data = request.data
my_time = datetime.datetime.strptime('07/05/15', '%m/%d/%y')
my_time = my_time.replace(hour=23, minute=59)
data['admin'] = admin
data['expire'] = my_time
data['msg_conv']['user'] = admin
serializer = ConversationSerializer(data=data)
if serializer.is_valid():
serializer.save()
return Response(data={'success': True, 'user': serializer.data}, status=status.HTTP_200_OK)
else:
return Response(data={'success': False, 'msg': serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
POST json
{
"subject":"aloha",
"conversation_type":1,
"patient":3,
"msg_conv":{
"text":"ovo je neki teks"
}
}
Can't figure out how to return data from serializer to view.
Django version : 1.10.2
Python: 3.4.3
The issue is there:
class ConversationSerializer(serializers.ModelSerializer):
msg_conv = MessagesSerializer()
By doing this, you are saying that Conversation has a FK to Message. Therefore DRF tries to do the mapping and fails because it's the oposit.
You just need to add the many=True argument to let DRF knows this is a reversed FK.

Categories

Resources