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.
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.
class User(AbstractUser):
email = models.EmailField(unique=True)
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.