at my models.py I have a "Movies" model with the following field setup:
video_stream_relation = GenericRelation(VideoStreamInfo, related_query_name='video_stream_relation')
This GenericRelation field points to the following model class:
class VideoStreamInfo(models.Model):
objects = RandomManager()
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
content_type = models.ForeignKey(ContentType, limit_choices_to=referential_stream_models, on_delete=models.CASCADE, verbose_name=_("Content Type"))
object_id = models.CharField(max_length=36, verbose_name=_("Object ID"))
content_object = GenericForeignKey('content_type', 'object_id')
index = models.IntegerField(verbose_name=_("Stream Index"), blank=False)
bit_rate = models.IntegerField(verbose_name=_("Bitrate (bps)"), blank=True, null=True, editable=False)
codec_name = models.CharField(verbose_name=_("Codec Name"), blank=True, null=True, editable=False, max_length=255)
width = models.IntegerField(verbose_name=_("Width"), blank=True, null=True, editable=False)
height = models.IntegerField(verbose_name=_("Height"), blank=True, null=True, editable=False)
date_added = models.DateTimeField(auto_now_add=True, verbose_name=_("Date Added"))
Now the Question is how can I get video_stream_relation.codec_name value in a ModelSerializer like this:
class MovieSerializer(serializers.ModelSerializer):
id = serializers.PrimaryKeyRelatedField(queryset=Movies.objects.all())
class Meta:
model = Movies
fields = ('id',
...)
I want to be able to display the codec_name as a API JsonResponse.
If needed, this is how my API view currently looks like:
#api_view(['GET',])
#authentication_classes([JSONWebTokenAuthentication])
#permission_classes([AllowAny])
def movies(request):
if request.method == 'GET':
obj = Movies.objects.all()
serializer = MovieSerializer(obj, many=True)
return JsonResponse(serializer.data, safe=False)
If I try to add the video_stream_relation field to my MovieSerializer I get back the following error:
TypeError: Object of type GenericRelatedObjectManager is not JSON
serializable
Thanks in advance.
You can create a model serializer for VideoStreamInfo and use it in MovieSerializer as a related manager like this:
from rest_framework import serializers
class VideoStreamInfoSerializer(serializers.ModelSerializer):
class Meta:
model = VideoStreamInfo
fields = ('codec_name', )
class MovieSerializer(serializers.ModelSerializer):
video_stream_relation = VideoStreamInfoSerializer(many=True, read_only=True)
id = serializers.PrimaryKeyRelatedField(queryset=Movies.objects.all())
class Meta:
model = Movies
fields = ('id',
'video_stream_relation',
...
)
Related
Here is my model and I want to register a Django admin for it. I want to display the related objects automatically when a user selects a content type. Do you have any solution for it?
class UserAccessContent(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
order = models.ForeignKey(Order, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True, null=True)
updated_at = models.DateTimeField(auto_now=True, null=True)
def __str__(self) -> str:
return self.content_object.title
and here is my admin class. It seems that autocomplete_lookup_fields doesn't work! I don't know why.
#admin.register(UserAccessContent)
class UserAccessContentAdmin(admin.ModelAdmin):
readonly_fields = ['content_object']
list_display = ['id', 'user', 'content_object', ]
list_display_links = ['id']
list_per_page = 20
search_fields = ['user']
autocomplete_lookup_fields = {
'content_object': [['content_type', 'object_id']],
}
I followed Dennis Ivy proshop Tutorial He used the same approach as the code is
class ReviewSerializer(serializers.ModelSerializer):
class Meta:
model = Review
fields = '__all__'
class ProductSerializer(serializers.ModelSerializer):
reviews = serializers.SerializerMethodField(read_only=True)
class Meta:
model = Product
fields = '__all__'
def get_reviews(self, obj):
reviews = obj.review_set.all()
serializer = ReviewSerializer(reviews, many=True)
return serializer.data
Now I need a Blog for the eCommerce Project and I created another app named blog and Created the models as
class BlogPost(models.Model):
_id = models.AutoField(primary_key=True, editable=False)
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
title = models.CharField(max_length=200, null=True, blank=True, help_text="Like How To Treat Hypertension etc")
image = models.ImageField(null=True, blank=True,
default='/placeholder.png')
rating = models.DecimalField(
max_digits=7, decimal_places=2, null=True, blank=True)
numReviews = models.IntegerField(null=True, blank=True, default=0)
createdAt = models.DateTimeField(auto_now_add=True)
youtubeVideoLink = models.CharField(max_length=1000, null=True , blank=True)
def __str__(self):
return str(self.createdAt)
class BlogPostReview(models.Model):
blogpost = models.ForeignKey(BlogPost, on_delete=models.SET_NULL, null=True)
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
name = models.CharField(max_length=200, null=True, blank=True)
rating = models.IntegerField(null=True, blank=True, default=0)
comment = models.TextField(null=True, blank=True)
createdAt = models.DateTimeField(auto_now_add=True)
_id = models.AutoField(primary_key=True, editable=False)
def __str__(self):
return str(self.rating)
But when I serialize them via same approach as mentioned above....
class BlogPostReviewSerializer(serializers.ModelSerializer):
class Meta:
model = BlogPostReview
fields = '__all__'
class BlogPostSerializer(serializers.ModelSerializer):
blog_post_reviews = serializers.SerializerMethodField(read_only=True)
class Meta:
model = BlogPost
fields = '__all__'
def get_blog_post_reviews(self, obj):
blog_post_reviews = obj.review_set.all()
serializer = BlogPostReviewSerializer(blog_post_reviews, many=True)
return serializer.data
This error comes
in get_blog_post_reviews
blog_post_reviews = obj.review_set.all()
AttributeError: 'BlogPost' object has no attribute 'review_set'
How to solve this problem or what I'm doing wrong and what need to be fixed. What would be another apporach obv there would be.... And I don't know why Dennis Ivy used review_set in his code. If someone know why we use _set and what are the circumstances please let me know.
The simplest solution is to update your get_blog_post_reviews method:
def get_blog_post_reviews(self, obj):
blog_post_reviews = obj.blogpostreview_set.all() # <- this line has changed
serializer = BlogPostReviewSerializer(blog_post_reviews, many=True)
return serializer.data
The original worked because there was a model named Review, so the automatically created reverse name was review_set. Your model is named BlogPostReview, so the reverse is blogpostreview_set.
More information about reverse relationships in the docs.
I'm having a problem with serializing the data with joined tables. Am i doing this right? i'm just a newbie in django.
here's my models.py
from django.db import models
from django.contrib.auth.models import AbstractBaseUser
class Agency(models.Model):
agency_id = models.CharField(primary_key=True, max_length=50)
agency_shortname = models.CharField(max_length=20, blank=True, null=True)
agency_longname = models.CharField(max_length=255, blank=True, null=True)
date_created = models.DateTimeField(blank=True, null=True)
class Meta:
managed = False
db_table = 'agency'
class Users(AbstractBaseUser):
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = []
user_id = models.CharField(primary_key=True, max_length=50)
first_name = models.CharField(max_length=50, blank=True, null=True)
middle_name = models.CharField(max_length=50, blank=True, null=True)
last_name = models.CharField(max_length=50, blank=True, null=True)
username = models.CharField(unique=True, max_length=50, blank=True, null=True)
password = models.CharField(max_length=100, blank=True, null=True)
agency = models.OneToOneField(Agency, models.DO_NOTHING)
date_created = models.DateTimeField(blank=True, null=True)
active = models.CharField(max_length=8)
login_status = models.CharField(max_length=6)
last_login = models.DateTimeField(blank=True, null=True)
class Meta:
managed = False
db_table = 'users'
Then in my serializers.py
from rest_framework import serializers
from .models import Users, Agency
class UserDetailSerializer(serializers.ModelSerializer):
class Meta:
model = Users
fields = '__all__'
class AgencyDetailSerializer(serializers.ModelSerializer):
agency_id = UserDetailSerializer()
class Meta:
model = Agency
fields = ['agency_id','agency_shortname','agency_longname']
and my views.py
from rest_framework.decorators import api_view, permission_classes
from rest_framework.response import Response
from .serializers import UserDetailSerializer
from .models import Users, Agency
from rest_framework.permissions import IsAuthenticated
#api_view(['GET'])
#permission_classes([IsAuthenticated])
def userData(request):
username = request.GET.get('username')
r_type = request.GET.get('type')
user = Users.objects.get(username=username).values('user_id','first_name','middle_name','last_name','username','email','agency__agency_id','agency__agency_shortname','agency__agency_longname')
user_serializer = UserDetailSerializer(user, many=False)
return Response(user_serializer.data)
I'm getting an error of 'Users' object has no attribute 'values', what seems be the problem? i'm building api based application with vue.js and django rest framework. Thanks a lot
get queryset does not have values attribute, using filter instead.
user = Users.objects.filter(username=username).values(...)
Regarding your case, using get is enough and custom your Serializer like this:
class UserDetailSerializer(serializers.ModelSerializer):
class Meta:
model = Users
fields = '__all__'
agency = AgencyDetailSerializer(many=False, read_only=True)
Because values is not an attribute of model object. You can call values on queryset object. something like:
user = Users.objects.filter(username=username).values('user_id','first_name','middle_name','last_name','username','email','agency__agency_id','agency__agency_shortname','agency__agency_longname')
Note: You don't need to call values, just change this line to:
from rest_framework.generics import get_object_or_404
...
user = get_object_or_404(Users, **dict(username=username))
user_serializer = UserDetailSerializer(user)
...
Because serializer itself will handle it for you.
Context
I have 2 models: App & AppVersion.
I am trying to serialize fields from AppVersion along with a field from the App model. I am unable to display the related field in my serializer.
Goal
My goal is to have the API response include the related field like this:
[
{
"app_version_uuid": "61ee8efa-f79e-4fcd-a6ea-4a33544442e1",
"app_version_name": "Test app version",
"version_code": 2,
"version_name": "0.2",
"auto_start": True # related field
}
]
Models
# models.py
class App(models.Model):
app_uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, db_index=True)
app_name = models.CharField(max_length=100)
package_id = models.CharField(max_length=100, unique=True, null=True, blank=True, editable=False)
auto_start = models.BooleanField(default=False)
class AppVersion(models.Model):
app_version_uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, db_index=True)
app_uuid = models.ForeignKey(App, on_delete=models.CASCADE, related_name='app_versions')
app_version_name = models.CharField(max_length=100)
version_name = models.CharField(blank=True, null=True, max_length=100, editable=False)
version_code = models.IntegerField(blank=True, null=True, editable=False)
Serializers
# serializers.py
class AppVersionSerializer(serializers.ModelSerializer):
auto_start = serializers.SlugRelatedField(read_only=True, slug_field='auto_start')
class Meta:
model = AppVersion
fields = ('app_version_uuid', 'app_version_name', 'version_code', 'version_name', 'auto_start')
What I tried
I tried to use the SlugRelatedField() as shown in my serializers.py. I also tried to use a SerializerMethodField() like this:
# serializers.py
class AppSerializer(serializers.ModelSerializer):
class Meta:
model = App
fields = ('auto_start')
class AppVersionSerializer(serializers.ModelSerializer):
auto_start = serializers.SerializerMethodField(read_only=True)
def get_auto_start(self, model):
return AppSerializer(model).data
class Meta:
model = AppVersion
fields = ('app_version_uuid', 'app_version_name', 'version_code', 'version_name', 'auto_start')
The SlugRelatedField() does not show the auto_start in the response.
Using the SerializerMethodField() auto_start is shown as:
"auto_start": {}
What do I need to change in order to get the related field auto_start in my response?
Use source argument along with BooleanField,
class AppVersionSerializer(serializers.ModelSerializer):
auto_start = serializers.BooleanField(source='app_uuid.auto_start')
class Meta:
model = AppVersion
fields = ('app_version_uuid', 'app_version_name', 'version_code', 'version_name', 'auto_start')
I have two models:
class Album(models.Model):
code = models.CharField(max_length=10, primary_key=True, default=_create_access_code, verbose_name=_("Id"))
name = models.CharField(max_length=200, verbose_name=_("Name"))
description = models.TextField(null=True, blank=True, verbose_name=_("Description"))
company = models.ForeignKey(Company, on_delete=models.PROTECT, related_name='albums', verbose_name=_("Company"))
access_code = models.CharField(max_length=10, default=_create_access_code, verbose_name=_("Internal Use"))
class Meta:
verbose_name = _("Album")
verbose_name_plural = _("Albums")
def __str__(self):
return "[{}] {} ({})".format(self.pk, self.name, self.company.id)
class Photo(models.Model):
name = models.CharField(max_length=100, null=True, blank=True, verbose_name=_("Name"))
album = models.ForeignKey(Album, on_delete=models.CASCADE, related_name='photos', verbose_name=_("Album"))
photo = models.ImageField(verbose_name=_("Photo"))
class Meta:
verbose_name = _("Photo")
verbose_name_plural =_("Photos")
def __str__(self):
return "[{}] {}".format(self.pk, self.name)
I am trying to make a post to the ModelViewSet for model Albums, but I get an error indicating that field photos is required. Even the OPTIONS method indicates it es required.
How can I instruct DRF for not considering look up fields as required? Is it some serializer setting?
You can add required=False to fields in the serializer.
photos = PhotoSerializer(many=True, required=False)
Something like this. Can you post you serializers?