AttributeError - object has no attribute 'crea

2019-09-20 14:59发布

I'm trying to save a through model which has the following attributes via Django-rest-framework

when sending a POST (I'm trying to create a new instance), I get the following error:

AttributeError at /api/organisation/provider/ 'EnabledExternalProvider' object has no attribute 'create'

any ideas as to what i'm doing incorrectly?

the through model in question is:

class EnabledExternalProvider(models.Model):
    provider = models.ForeignKey(ExternalProvider, related_name='associatedProvider')
    organisation = models.ForeignKey(Organisation, related_name='associatedOrg')
    enabled = models.BooleanField(default=True)
    tenantIdentifier = models.CharField('Tenant identifer for organisation', max_length = 128, null=True, blank=True)
    def __str__(self):
        return self.provider + '-' + self.organisation

my view is:

class EnabledExternalProvider(mixins.RetrieveModelMixin, mixins.UpdateModelMixin,generics.GenericAPIView):
    serializer_class = ConnectedServiceSerializer

def get_queryset(self):
    return EnabledExternalProvider.objects.filter(organisation=self.request.user.organisation_id)

def get_object(self):
    queryset = self.filter_queryset(self.get_queryset())
    # make sure to catch 404's below
    obj = queryset.get(organisation=self.request.user.organisation_id)
    self.check_object_permissions(self.request, obj)
    return obj

def get(self, request, *args, **kwargs):
    return self.retrieve(request, *args, **kwargs)

def post(self, request, *args, **kwargs):
    return self.create(request, *args, **kwargs)

and my serializer is:

class ConnectedServiceSerializer(serializers.ModelSerializer):
    provider=ExternalProviderSerializer(read_only=True)
    organisation=OrganisationDetailSerializer(read_only=True)
    class Meta:
        model = EnabledExternalProvider
        fields = ( 'organisation', 'enabled', 'tenantIdentifier')
        read_only_fields = ('organisation', 'provider')

I'm POSTing the following:

{"provider":"1","tenantIdentifier":"9f0e40fe-3d6d-4172-9015-4298684a9ad2","enabled":true}

1条回答
时光不老,我们不散
2楼-- · 2019-09-20 15:26

Your view doesn't have that method because you haven't defined it, or inherited from a class that has it; your mixins provide retrieve and update, but not create.

You could add mixins.CreateModelMixin to the inheritance, but at this point you should really be using a ViewSet instead.

查看更多
登录 后发表回答