Got AttributeError when attempting to get a value for field `name` on serializer `BusinessSerializer`
I am getting this error. when trying to save a record using rest
AttributeError at /business/
Got AttributeError when attempting to get a value for field name
on serializer BusinessSerializer
.
The serializer field might be named incorrectly and not match any attribute or key on the User
instance.
Original exception text was: 'User' object has no attribute 'name'.
why is trying to find name field on user object when business object already has that field
views.py
class BusinessViewSet(viewsets.ModelViewSet):
serializer_class = BusinessSerializer
queryset = Business.objects.all()
def create(self,request):
serializer = BusinessSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors)
def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
businessRecord = serializer.save()
return Response({
"business": BusinessSerializer(businessRecord, context=self.get_serializer_context()).data,
})
models.py
class Business(models.Model):
businessOwner = models.OneToOneField(User,blank=True,null=True,on_delete=models.CASCADE) # first name, last name, username, email, password etc. present in this model
name = models.TextField()
mobileNumber = PhoneField(help_text='Mobile Number')
businessLocation = models.PointField()
created = models.DateTimeField(auto_now_add=True)
businessType = models.CharField(max_length = 30,choices = BUSINESS_TYPE_CHOICES,default = 'Business')
address = models.TextField()
businessCategory = models.ManyToManyField(Category, related_name='Bcategory', blank=True)
hashTags = models.ManyToManyField(HashTag, related_name='Bhashtag', blank=True)
socialMediaAccountLink = models.URLField(max_length=400, blank=True,null=True)
socialMediaType = models.CharField(max_length=30, choices=SOCIAL_MEDIA_TYPE_CHOICES, default='Instagram')
class Meta:
indexes = [
models.Index(fields=['businessLocation', 'businessType','name','businessOwner']),
]
serializers.py
class BusinessOwnerSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('username', 'email', 'password','first_name','last_name')
extra_kwargs = {'username': {'allow_null': True,'allow_blank': True},
'email': {'allow_null': True,'allow_blank': True},
'password': {'allow_null': True,'allow_blank': True},
'first_name': {'allow_null': True,'allow_blank': True},
'last_name': {'allow_null': True,'allow_blank': True}}
class BusinessSerializer(serializers.ModelSerializer):
offers = serializers.SerializerMethodField('get_offers', allow_null=True)
posts = serializers.SerializerMethodField('get_reviewposts', allow_null=True)
medias = serializers.SerializerMethodField('get_business_media', allow_null=True)
Owner = BusinessOwnerSerializer(allow_null=True)
def get_offers(self,request):
if request=="GET":
offers = Offer.objects.filter(business=self)
serializer = OfferSerializer(instance=offers, context=self.context, many=True, read_only=True)
return serializer.data
def get_reviewposts(self,request):
if request == "GET":
posts = Post.objects.filter(business=self).filter(postType='Review')
serializer = PostSerializer(instance=posts, context=self.context, many=True, read_only=True)
return serializer.data
def get_business_media(self,request):
if request == "GET":
media = BusinessMedia.objects.filter(business=self).filter(postType='Review')
serializer = BusinessMediaSerializer(instance=media, context=self.context, many=True, read_only=True)
return serializer.data
class Meta:
model = Business
extra_kwargs = {'password': {'write_only': True},'Owner': {'allow_null': True,'allow_blank': True}}
#fields = "__all__"
exclude = ["businessOwner"]
def create(self, validated_data):
if validated_data['Owner']['username'] != '':
user = User.objects.create_user(validated_data['Owner']['username'], email= validated_data['Owner']['email'], password= validated_data['Owner']['password'])
return user
return User()
i tried finding relevant solutions none worked
I don't know if it's relevant. But you shouldn't be returning User
instances from BusinessSerializer.create
. It should be returning Business
instances (e.g. look at this). It could be that Django is trying to serialize a User
instance into a Business
instance and failing with a weird error message. I'd start from there and see where that gets you.
Also, Django offers tracebacks that could help in your case. Try taking a look at that, or posting it here for reference.
Moreover, you can use a debugger and see what data you have accessible in your create
method. Just add an import pdb; pdb.set_trace()
statement and try creating a record as you previously did. You'll see what's going on.