# SerializerMethodField Issue

**URL:** <https://forum.djangoproject.com/t/serializermethodfield-issue/19089>\
**Category:** Forms & APIs\
**Created:** [February 24, 2023, 5:59pm UTC](https://forum.djangoproject.com/t/serializermethodfield-issue/19089 "2023-02-24T17:59:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anthonynarine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anthonynarine/32/12389_2.png) [@anthonynarine](https://forum.djangoproject.com/u/anthonynarine)\
**Post date:** [February 24, 2023, 5:59pm UTC](https://forum.djangoproject.com/t/serializermethodfield-issue/19089/1 "2023-02-24T17:59:01Z")

</div>

Hi I’m very new to Django/programming and this may be a trivial error but I just can’t seem to get past it.

I am trying to display a username to my front end (currently testing backend with postman) I was able to add a state field by using SerializerMethodField() but I am unable to do the same with the user name. In my Listing model I added a seller field that points to a Django’s User model. Django is throwing a attribute error "object has no attribute “username”.

Below is my Listing model with the seller field pointing to the User.

```auto
Class Listing(models.Model):
    # sellers and users are the same so the fk will point to the User model.
    seller = models.ForeignKey(
        User, on_delete=models.CASCADE, blank=True, null=True)
    title = models.CharField(max_length=150)
    description = models.TextField(null=True, blank=True)
    choices_area = (
        ("Brooklyn", "Brooklyn"),
        ("Queens", "Queens"),
        ("Bronx", "Bronx"),
        ("Manhattan", "Manhattan"),
        ("Statin Island", "Statin Island"),
    )
    area = models.CharField(max_length=25, blank=True, null=True, choices=choices_area)
    choices_listing_type = (
        ("House", "House"),
        ("Appartment", "Appartment"),
        ("Office", "Office"),
        ("Commercial space", "Commercial Space"),
        ("Parking Space", "Parking Space"),
    )
    listing_type = models.CharField(max_length=20, choices=choices_listing_type)
    choices_propery_status = (("Sale", "Sale"), ("Rent", "Rent"))
    property_status = models.CharField(
        max_length=25, blank=True, null=True, choices=choices_propery_status
    )
    price = models.DecimalField(max_digits=50, decimal_places=0)
    choices_rental_frequencey = (
        ("Month", "Month"),
        ("Week", "Week"),
        ("Day", "day"),
    )
    rental_frequency = models.CharField(
        max_length=20, blank=True, null=True, choices=choices_rental_frequencey
    )
    rooms = models.IntegerField(blank=True, null=True)
    furnished = models.BooleanField(default=False)
    pool = models.BooleanField(default=False)
    elevator = models.BooleanField(default=False)
    parking = models.BooleanField(default=False)
    date_posted = models.DateTimeField(default=timezone.now)
    latitude = models.FloatField(blank=True, null=True)
    longitude = models.FloatField(blank=True, null=True)
    # LOCATION FIELD WILL FUNCTIONALIY MOVED TO FRONT END
    # location = models.PointField(blank=True, null=True, srid=4326)
    picture1 = models.ImageField(blank=True, null=True, upload_to="pictures/%Y/%m/%d/")
    picture2 = models.ImageField(blank=True, null=True, upload_to="pictures/%Y/%m/%d/")
    picture3 = models.ImageField(blank=True, null=True, upload_to="pictures/%Y/%m/%d/")
    picture4 = models.ImageField(blank=True, null=True, upload_to="pictures/%Y/%m/%d/")
    picture5 = models.ImageField(blank=True, null=True, upload_to="pictures/%Y/%m/%d/")

```

My User model.

```auto
class User(AbstractUser):
    email = models.EmailField(unique=True)

```

```auto
class ListingSerializer(serializers.ModelSerializer):  
    # the fild you want to add is alway set up with the name of the field then serializers.SerializerMethodField() 
    state = serializers.SerializerMethodField() 
    # seller = serializers.SerializerMethodField()
    # seller_agency_name = serializers.SerializerMethodField()
    seller_username = serializers.SerializerMethodField()
    
    # def seller_agency_name(self, obj):
    # return obj.seller.profile.agency_name
        
    def get_seller_username(self, obj):
        return obj.seller.username
         
#self is an instance of the class object and obj is the serialize data obj.
    def get_state(self, obj):
        return "New York"
    class Meta:
        model= Listing
        fields = " __all__"

```

any help would be greatly appreciated. please let me know if additional info is needed.

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [February 24, 2023, 6:39pm UTC](https://forum.djangoproject.com/t/serializermethodfield-issue/19089/2 "2023-02-24T18:39:49Z")

</div>

When posting code here surround it using the ` character so it gets properly formatted.

You’re talking about a SerializerMethod. Please put the your serializer definition here as well.

---

<div class="post-metadata">

**Author:** ![anthonynarine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anthonynarine/32/12389_2.png) [@anthonynarine](https://forum.djangoproject.com/u/anthonynarine)\
**Post date:** [February 24, 2023, 7:02pm UTC](https://forum.djangoproject.com/t/serializermethodfield-issue/19089/3 "2023-02-24T19:02:36Z")

</div>

thanks for the tips, I was able to update the post, you are right much more readable. I also added my model’s serializer. I am still trying to work through this error.

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [February 27, 2023, 10:24am UTC](https://forum.djangoproject.com/t/serializermethodfield-issue/19089/4 "2023-02-27T10:24:14Z")

</div>

Alright.  
Please also post the complete traceback that you’re receiving, also surrounded by the backticks `
