How i get the username from User?
You don’t want to - at least not as a separate field within the model. There’s no value in duplicating the data already present in the User object. What you want to do is reference the username field through the User object at the point in time when you need it.
Side note: Please do not post screen shots or images of code. Copy / paste the code itself directly into the text of your message (or comment) surrounded by lines of three backtick - ` characters. You’ll have a line of ```, then your code, then another line of ```.
How i reference the username field? so i can send that to my serializer
If you have an instance of a User
object, how would you access the username
field?
example, assume you have a reference to a User
object named a_user
defined as follows:
a_user = User.objects.get(id=10)
(assuming you have a User object with an id of 10)
How would you access the username
field of a_user
?
a_user.username
author = models.ForeignKey(User, on_delete = models.CASCADE)
But this is giving me just the id not the object
Exactly.
So, if the field named author
is a reference to a User
object, how would you access the username
field of author
?
What are you looking that that is giving you that impression?
When in my in my API i get
HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
[
{
"id": 1,
"author": 1,
"title": "PRETEMPORADA 2022: PRIMER GOLPE",
"body":
Ok, so it’s your API that is the issue, not the model.
What is the view that is generating that output?
views.py
class PostList(generics.ListCreateAPIView):
queryset = Post.objects.all()
serializer_class = PostSerializer
serializers.py
class PostSerializer(serializers.ModelSerializer):
class Meta:
fields = ('id', 'author', 'title', 'body', 'created_at')
model = Post
Depending upon how you want your output structured, you can:
- Allow for DRF to create the nested reference. (See Specifying nested serialization)
- Add an extra field to the serializer for the related data (See Specifying fields explicitly)
- Create a hyperlinked reference to the User object (See HyperlinkedModelSerializer if you’re interested. You probably aren’t, but I’m including it here for being relatively complete.)
See Serializer relations - Django REST framework for more details and options for how this can be handled. It comes down to how you want this represented by your API.
i have the username now but the password too
HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
[
{
"id": 1,
"author": {
"id": 1,
"password":
i think thats not a good info to past to my frotend
Am gonna check the other options
You can always specify what fields to include or exclude.
from rest_framework import serializers
from .models import Post
class PostSerializer(serializers.ModelSerializer):
author = serializers.CharField(source='author.username', read_only=True)
class Meta:
fields = ('id', 'author', 'title', 'body', 'created_at')
model = Post
depth = 1
its working, Thanks for the help