Field name user_username is not valid for model Profile

hellofanengineer

Error Name: Field name user_username is not valid for model Profile

I'm building my Edit Profile View.

Here is my views.py

class ProfileEditAPIView(DestroyModelMixin, UpdateModelMixin, generics.RetrieveAPIView):
    serializer_class = ProfileEditSerializer

    def get_queryset(self):
        logged_in_user = User.objects.filter(username=self.request.user.username)
        return logged_in_user

    def get_object(self):
        queryset = self.get_queryset()
        obj = get_object_or_404(queryset)
        return obj.profile

    def put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)

    def delete(self, request, *args, **kwargs):
        return self.destroy(request, *args, **kwargs)

I can get user_id correctly but somehow I can't access to its username field

This is serializers.py

class ProfileEditSerializer(serializers.ModelSerializer):
    class Meta:
        model = Profile
        fields = (
            'user_username', <<<
            'title',
            'gender',
            'birth',
            'height',
            'height_in_ft',
            'profile_img',
        )

models.py

class Profile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL)
    title = models.TextField(max_length=155, blank=True)
    gender = models.CharField(max_length=10, choices=GENDER_CHOICES, default='u') # Recommend Factor
    location = models.CharField(max_length=40, choices=LOCATION_CHOICES, default='ud') # Recommend Factor
    birth = models.DateField(default='1992-07-23', blank=True, null=True) # Recommend Factor
    height = models.CharField(max_length=5, default='undefined')
    height_in_ft = models.BooleanField(default=True)
    profile_img = models.ImageField(
        upload_to=upload_location,
        null=True,
        blank=True)

Why can't we access to user's username? And how can we solve this?

Thanks

Astik Anand

You can get the username from User model using this way.

class ProfileEditSerializer(serializers.ModelSerializer):
    username = serializers.CharField(read_only=True, source="user.username")

    class Meta:
        model = Profile
        fields = (
            'username',
            'title',
            . . . .
        )

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Django-Rest-Framework 3.0 Field name '<field>' is not valid for model `ModelBase`

Multiple USERNAME_FIELD in django user model

Field name `username` is not valid for model

django.core.exceptions.ImproperlyConfigured: Field name `id` is not valid for model

Django Rest Framework: field name 'likes' is not valid for model 'userPost' improperlyConfigured

Using the user_username instead of user_id to access a user profile

Django - Return full_name from Profile model which has relation with Django User model in other model

Django custom User model throwing SystemCheckError - The field 'username' clashes with the name 'username'

Django update "extended user profile" model field from webhook paramater

field name choice 'reference' is not a valid choice issue

Django Auth User Model Set different field name for password

How do I add username of logged in user to model field in django

Django model with User, Profile and company

Compare Django model field and request.user.username in HTML

Querying a User Profile model

Yii Model CGridView including column with Yii-User Profile Field data

Added Bio field to 'edit profile' - created migration but then what to add to model?

Changing name from "profile" to the WordPress username

Show variable from different model as a field in Profile in DJANGO Admin

user reference in the profile model or reference of the profile in the user model

Login user for a custom user model with email as username field

ImproperlyConfigured: Field name first_name is not valid for model CustomUser?

RelatedObjectDoesNotExist at /register/ User has no profile for Extended User Model and Profile

Django model owner field reading the log in user as None instead of the username

Update user Profile in asp.net mvc model is not valid

Field name `get_thumbnail` is not valid for model `Product`

How to change the field of username to user_name in to django custom user model?

ImproperlyConfigured at /books/ Field name `Published_date` is not valid for model `Books`

Django rest framework Field name `ia_superuser` is not valid for model `CustomUser`