Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

python - Django RestAPI 500 error when trying to GET a list of records

What I am trying to achieve: Is get a simple list of ALL links FOR THE logged in User - using this end point : http://127.0.0.1:8000/api/affypartnerlinks/

The list should include ALL of the fields (including key) from the USERaffylinks model table PLUS the associated site_name from the USERaffiliates table - but only for the current logged in user.

CURL test

curl -X GET http://127.0.0.1:8000/api/affypartnerlinks/ -H 'Authorization: Token afdf84a95d53e15aea484b011fd3c754b165c1f2'

This is the error I am getting.

500 Error: AttributeError: Got AttributeError when attempting to get a value for field owner_link_useraffyid on serializer USERaffylinksSerializer. The serializer field might be named incorrectly and not match any attribute or key on the USERaffiliates instance. Original exception text was: 'USERaffiliates' object has no attribute 'owner_link_useraffyid'.

--- Thank you :)

VIEWS.py

@action(methods=['POST','GET',], detail=True)
class AffyPartnerLinksViewSet(viewsets.ModelViewSet):
    queryset= USERaffylinks.objects.all()  #need to filter on login user > owneruserid=request.user.id
    serializer_class = USERaffylinksSerializer
    authentication_classes = (TokenAuthentication,)
    permission_classes = (IsAuthenticated, )
  
    def get_queryset(self):
        #restrict to current logged in  user
        user = self.request.user
        return USERaffiliates.objects.filter(owneruserid=user)

SERIALIZER.py

class USERaffylinksSerializer(serializers.ModelSerializer):
    class Meta:
        model = USERaffylinks
        fields = '__all__'
        extra_fields = ['site_name']        #from USERaffiliates table
        extra_kwargs = {
            'owneruserid': {'required':True},
            'owner_link_useraffyid': {'required':True}
        }

    def get_field_names(self, declared_fields, info):
        expanded_fields = super(USERaffylinksSerializer, self).get_field_names(declared_fields, info)

        if getattr(self.Meta, 'extra_fields', None):
            return expanded_fields + self.Meta.extra_fields
        else:
            return expanded_fields

MODELS.py


class USERaffylinks(models.Model):
    owner_link_useraffyid = models.ForeignKey(USERaffiliates, on_delete=models.CASCADE) #link(s) for each partner stores the  id from USERaffiliates
    owneruserid = models.ForeignKey(User, on_delete=models.CASCADE, default=0) #added to make the model easier to access link data when we only have userID

    ...
    ...

    def site_name(self):
        return self.owner_link_useraffyid.site_name   



class USERaffiliates(models.Model):
    owneruserid = models.ForeignKey(User, on_delete=models.CASCADE, default=1) #if no registered user admin owns = 1
    user_category = models.BigIntegerField(null=True, default=None, blank=True ) #id key from USERcategories
    registered_email = models.EmailField(null=True, default=None, blank=True)
    site_name = models.CharField(max_length=40, null=True)  #partner name field

    ...
    ...
question from:https://stackoverflow.com/questions/65878193/django-restapi-500-error-when-trying-to-get-a-list-of-records

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In AffyPartnerLinksViewSet.get_queryset you are returning a QuerySet of USERaffiliates instances, the viewset and serializer are however expecting a QuerySet of USERaffylinks instances.

You need to modify this method to return a QuerySet for the correct model

def get_queryset(self):
    #restrict to current logged in  user
    user = self.request.user
    return USERaffylinks.objects.filter(owneruserid=user)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...