I'm new to Django I want to get profile image from another model topic
models.py
class UserProfile(models.Model):
user = models.OneToOneField(User)
file = models.ImageField(upload_to='profile_image', blank=True)
def __unicode__(self):
return u'%s' % self.user
class Topics(models.Model):
user = models.ForeignKey(User)
title = models.charField(max_length = 55)
serializers.py
User = get_user_model()
class pic(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields = ['file']
class UserInfo(serializers.ModelSerializer):
username = pic(read_only=True)
class Meta:
model = User
fields = ['username','first_name',]
class TopicSerializer(serializers.ModelSerializer):
user= UserInfo(read_only=True)
class Meta:
model=Topics
fields = ('user','title',)
I'm getting like this:
"user": {
"username": {},
"first_name": ""
},
"title": "Django the title",
Now I need every file field related to Topics field with the first name and email of the user.
I want to like this:
"user": {
"username": "akash",
"first_name": "Akash DK"
"file":"static/imag.png"
},
"title": "Django the title",
Thanks in advance
You could use a SerializerMethodField in your UserInfo serializer to lookup the profile image and add it to the output data.
class UserInfo(serializers.ModelSerializer):
file = serializers.SerializerMethodField()
def get_file(self, user):
return UserProfile.objects.get(user=user).file.url
class Meta:
model = User
fields = ['username','first_name', 'file']
Related
I have 2 models one for a user and another for associated files for the user
Models.py
class IndividualUser(models.Model):
membership_id = models.CharField(primary_key=True, max_length=100, default=1)
profile_image = models.ImageField(blank=True, upload_to ="individual_member_profile/", null=True)
firstname = models.CharField(max_length=100)
lastname = models.CharField(max_length=100)
class MemberCatalogue(models.Model):
membership_id = models.ForeignKey(IndividualUser, default=None,on_delete=models.CASCADE, related_name="member_catalogue")
files = models.FileField(upload_to="individualmembercatalogue/")
Currently Iam getting absolute path for files. I need a filename uploaded by user for which Iam trying to get "files" field and and then split it with "/". But Iam struggling to get the files field from MemberCatalogue.
My serializers look like this at the moment:
class MemberCatalogueSerializer(serializers.ModelSerializer):
files = serializers.FileField()
class Meta:
model = MemberCatalogue
fields = ['id','membership_id', 'files']
class IndividualMembersSerializer(serializers.ModelSerializer):
member_catalogue = MemberCatalogueSerializer(many=True)
filename = serializers.SerializerMethodField('get_filename')
def get_filename(self, obj):
return obj.individualmember.files
class Meta:
model = IndividualMembers
fields = "__all__"
But I cant get the desired output.
Expected output is like
{
"membership_id": "142369ca1b1484d8d9d6d87fdc8543db",
"member_catalogue": [
{
"id": 156,
"membership_id": "142369ca1b1484d8d9d6d87fdc8543db",
"files": "http://127.0.0.1:8000/individualmembercatalogue/some_file.pdf"
"filename" : "some_file.pdf"
}
],
"profile_image": null,
"firstname": "John",
"lastname": "Doe",
}
You are using the wrong model in your IndividualMembersSerializer.
I think you are trying to edit the way filename is displayed in the wrong serializer, since files is a field on MemberCatalogue and not on IndividualUser.
Here is how I would do it:
class MemberCatalogueSerializer(serializers.ModelSerializer):
filename = serializers.SerializerMethodField()
def get_filename(self, obj):
return str(obj.files).split('/')[1]
class Meta:
model = MemberCatalogue
fields = ['id','membership_id', 'files', 'filename']
class IndividualMembersSerializer(serializers.ModelSerializer):
member_catalogue = MemberCatalogueSerializer(many=True)
class Meta:
model = IndividualUser
fields = "__all__"
class MemberCatalogueSerializer(serializers.ModelSerializer):
filename = serializers.SerializerMethodField()
class Meta:
model = MemberCatalogue
fields = ['membership_id', 'files','filename']
def get_filename(self, obj):
return obj.files.url.split('/')[-1]
I want to retrieve Student user fields, but I am having a hard time
trying to figure out how to do that I tried reading docs but still
confused
Studentserializer
class StudentSerializer(serializers.Serializer):
class Meta:
model = Student
fields = ['id', 'user', 'name', 'course']
def create(self, validated_data):
Student_det = Student.objects.create(**validated_data)
return Student_det
serializer view
def Student_list(request):
if request.method == 'GET':
stud = Student.objects.all()
serialized = StudentSerializer(stud, many=True)
print(serialized.data)
return JsonResponse(serialized.data, safe=False)
Student Model
class Student (models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
name = models.CharField(max_length=255)
course = models.CharField(max_length=255)
You can check here. I think the part about nested serializers is what you need.
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ("id", "first_name", "last_name", ) # and you can add other fields here "email", "date_joined", etc.
class StudentSerializer(serializers.ModelSerializer):
user = UserSerializer()
class Meta:
model = Student
fields = ("id", "user", "name", "course", )
This will serialize your Student model like
{
"id": 1,
"user": {
"id": 1,
"first_name": "Example",
"last_name": "User"
}
"name": "student name",
"course": "course name"
}
I am using generics to represent views
class PersonRetrieveView(generics.ListAPIView):
queryset = Person.objects.all()
serializer_class = PersonSerializer
and
class CommentRetrieveView(generics.RetrieveAPIView):
queryset = Comment.objects.all()
serializer_class = CommentSerializer
Person data looks like
{
"id": 2,
"user": {
"first_name": "Testuser",
"last_name": "TestUser1",
"id": 2
},
"name": "Test 2",
"city": "California",
"status": "NotActive",
"phone_number": "9876543222",
"age": 22,
"height": 180
}
and Comment
{
"id": 1,
"comment": "test",
"person": 2
}
Comment linked to Person by id. How can i add data from comment to PersonRetrieveView ?
Serializers looks like this
class PersonSerializer(serializers.ModelSerializer):
user = UserSerializer()
# comment = CommentSerializer()
class Meta:
model = Person
fields = '__all__'
class CommentSerializer(serializers.ModelSerializer):
class Meta:
model = Comment
fields = '__all__'
Model looks like
class Person(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=20)
city = models.CharField(max_length=20)
status = models.CharField(max_length=9, default='NotActive')
phone_number = models.CharField(max_length=10)
class Comment(models.Model):
person = models.ForeignKey(Person, on_delete=models.CASCADE)
comment = models.CharField(max_length=255)
You have Many-to-one relationship and you did not set related_name on Foreign key so your comments are accessible through comment_set which can be used to nest serializer
class PersonSerializer(serializers.ModelSerializer):
user = UserSerializer()
comments = CommentSerializer(source='comment_set', many=True)
class Meta:
model = Person
fields = [ 'user', 'city', 'comments', ...]
Provided that your Person model has a relation to Comment models, simply add depth = 1 in your Person serializer as follows:
class PersonSerializer(serializers.ModelSerializer):
user = UserSerializer()
class Meta:
model = Person
fields = '__all__'
depth = 1
i created a model named 'Post'.
here is the code:
class Post(models.Model):
body = models.TextField(max_length=10000)
date = models.DateTimeField(default=datetime.now, blank=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
class Meta:
ordering = ['-date']
i want to get all objects of Post model with users firstname and lastname.
in views.py:
#api_view(['GET'])
#permission_classes((IsAuthenticated,))
def allPost(request):
allpost = Post.objects.all()
serializer = PostSerializers(allpost, many=True)
return Response(serializer.data)
in serialisers.py:
class UserSerializers(serializers.ModelSerializer):
class Meta:
model = User
fields = '__all__'
class PostSerializers(serializers.ModelSerializer):
user = serializers.RelatedField(many=True)
class Meta:
model = Post
fields = ('body','date','user')
You can make a serialzier with firstname and lastname:
class SimpleUserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('first_name', 'last_name')
and then use that serializer as subserialiser:
class PostSerializer(serializers.ModelSerializer):
user = SimpleUserSerializer()
class Meta:
model = Post
fields = ('body','date','user')
This generates a JSON blob like:
{
"body": "Sample body text",
"date": "2020-12-11T12:34:56.789Z",
"user": {
"first_name": "MyFirst",
"last_name": "MyLast"
}
}
or you use make use of two CharFields:
class PostSerializer(serializers.ModelSerializer):
first_name = serializers.CharField(source='user.first_name', read_only=True)
last_name = serializers.CharField(source='user.last_name', read_only=True)
class Meta:
model = Post
fields = ('body','date')
this generates as JSON blob:
{
"body": "Sample body text",
"date": "2020-12-11T12:34:56.789Z",
"first_name": "MyFirst",
"last_name": "MyLast"
}
Note: The name of a serializer class is normally singular, so PostSerializer instead of
PostSerializers.
There is no need to use related field in this case. many=True should be used if you're passing multiple objects (like you did with posts).
What you should do is user your UserSerializer in PostSerializer
class PostSerializer(serializers.ModelSerializer):
user = UserSerializer()
class Meta:
model = Post
fields = ('body','date','user')
Read docs for more info
https://www.django-rest-framework.org/api-guide/relations/#nested-relationships
Let's say that we have models like below
class Movie(models.Model):
"""Stores a single movie entry."""
title = models.CharField(max_length=200, blank=False)
class Watchlist(models.Model):
"""Stores a user watchlist."""
user = models.ForeignKey(settings.AUTH_USER_MODEL,
related_name='watchlist',
on_delete=models.CASCADE)
movie = models.ForeignKey(Movie, related_name='watchlist',
on_delete=models.CASCADE)
added = models.BooleanField(default=False)
Serializer
class CustomUserSerializer(serializers.HyperlinkedModelSerializer):
"""Serializer for a custom user model with related user action."""
url = serializers.HyperlinkedIdentityField(
view_name='customuser-detail', lookup_field='username')
watchlist = serializers.HyperlinkedRelatedField(
many=True, view_name='watchlist-detail', read_only=True)
class Meta:
model = CustomUser
fields = ('url', 'username', 'watchlist')
and the view:
class CustomUserViewSet(viewsets.ReadOnlyModelViewSet):
"""
list:
Return a list of all the existing users.
retrieve:
Return the given user with user's watchlist.
"""
queryset = CustomUser.objects.all()
permissions = (IsAdminOrReadOnly)
lookup_field = 'username'
serializer_class = CustomUserSerializer
That all will give us a user and hyperlinked filed to the particular watchlist.
{
"url": "http://127.0.0.1:8000/api/v1/users/John/",
"username": "John",
"favorites": [
"http://127.0.0.1:8000/api/v1/watchlist/2/",
"http://127.0.0.1:8000/api/v1/watchlist/1/"
]
},
but instead of that I would like to get a particular movie instance like that.
{
"url": "http://127.0.0.1:8000/api/v1/users/John/",
"username": "John",
"favorites": [
"http://127.0.0.1:8000/api/v1/movies/33/",
"http://127.0.0.1:8000/api/v1/movies/12/"
]
},
so my question is how can I achieve that? I tried with hyperlinkedrelatedfield but nothing seems to work as expected.
You could use the SerializerMethodField along with reverse.
from rest_framework.reverse import reverse
class CustomUserSerializer(serializers.HyperlinkedModelSerializer):
"""Serializer for a custom user model with related user action."""
url = serializers.HyperlinkedIdentityField(
view_name='customuser-detail', lookup_field='username')
favorites = serializers.SerializerMethodField()
def get_favorites(self, obj):
movie_urls = [
reverse("movie-view", args=[watchlist.movie.id], request=self.context['request'])
for watchlist in obj.watchlist.all()
]
return movie_urls
class Meta:
model = CustomUser
fields = ('url', 'username', 'favorites')