0

I'm implementing a rest api using django-tastypie. My api resource is defined as follows:

class AddressResource(ModelResource):

    class Meta:
        resource_name = 'address'
        queryset = Address.objects.all()
        always_return_data = True
        authorization = Authorization()
        serializer = Serializer(formats=['json'])

        validation = Validation()

I have a model Address defined as:

class Address(models.Model):
    number = models.IntegerField()
    street = models.CharField(max_length=100)
    city = models.CharField(max_length=100)
    country = models.CharField(max_length=25)
    postalCode = models.CharField(max_length=5)

I can create Address resources posting to the url http://mydomain.com/api/v1/Address/. After Address resource creation the resource uri is /api/v1/Address/1/..../api/v1/Address/2/....etc

If I delete the resources directly from the model database or by doing a HTTP DELETE of the resource http://mydomain.com/api/v1/Address/2/, when I do a new post of a resource the id of the resource uri is still incrementing based on the last index.

Example: I have 30 address resources and I delete all of them, when I do a new post of a new resource the resource uri is /api/v1/Address/31/ instead of 1.

How can I delete the index when a resource is deleted?

Thanks in advance Victor

4

1 回答 1

0

如果您使用自动增量字段,这是不可能的,这是 django 的默认主键字段。要执行您想要的操作,您需要使用 primary_key=True 属性为主键指定您自己的字段。当你创建一个新的模型实例时,你需要手动将它设置为你想要的。

于 2013-05-15T11:43:21.693 回答