How to get a serialized value of owner Django REST framework when you create?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
24 views
Please tell me how using Django REST framework to get the owner of the model? There is a User model:
class User(AbstractBaseUser, PermissionsMixin):
a model Seeker associated with the User:
class Seeker(models.Model): user = models.OneToOneField(settings.AUTH_USER_MODEL)

is the model associated with Seeker:
class Education(CreatedModified): seeker = models.ForeignKey(Seeker)

when creating a serializer class EducationSerializerCreate I need to get seeker field associated with the current user session, and to substitute it by default in the serializable model
now the code is like this:
class EducationSerializerCreate(serializers.ModelSerializer): user = serializers.HiddenField(default=serializers.CurrentUserDefault()) seeker = Seeker.objects.filter(user=user) class Meta: model = Education fields = '__all__'

accordingly, Python complains:
TypeError: int() argument must be a string, a bytes-like object or a number, not 'HiddenField'
tell me how to do?
by | 24 views

1 Answer

0 like 0 dislike
First, the User is serializable, then the serialized User put into the serialization Education
class UserSerializer(serializers.ModelSerializer): class Meta: model = User fields = '__all__' class EducationSerializerCreate(serializers.ModelSerializer): user = UserSerializer() seeker = Seeker.objects.filter(user=user) class Meta: model = Education fields = '__all__'
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
110,608 questions
257,187 answers
0 comments
40,796 users