Skip to content

Commit

Permalink
Add support for BigAutoField in ModelResource (#1621)
Browse files Browse the repository at this point in the history
  • Loading branch information
TDaulat committed Jun 3, 2021
1 parent 3b435be commit ac18543
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion tastypie/resources.py
Expand Up @@ -1910,7 +1910,7 @@ def api_field_from_django_field(cls, f, default=fields.CharField):
result = fields.FloatField
elif internal_type in ('DecimalField',):
result = fields.DecimalField
elif internal_type in ('IntegerField', 'PositiveIntegerField', 'PositiveSmallIntegerField', 'SmallIntegerField', 'AutoField', 'BigIntegerField'):
elif internal_type in ('IntegerField', 'PositiveIntegerField', 'PositiveSmallIntegerField', 'SmallIntegerField', 'AutoField', 'BigIntegerField', 'BigAutoField'):
result = fields.IntegerField
elif internal_type in ('FileField', 'ImageField'):
result = fields.FileField
Expand Down
6 changes: 6 additions & 0 deletions tests/core/migrations/0001_initial.py
Expand Up @@ -114,4 +114,10 @@ class Migration(migrations.Migration):
],
bases=('core.note',),
),
migrations.CreateModel(
name='BigAutoNowModel',
fields=[
('id', models.BigAutoField(primary_key=True, serialize=False, verbose_name='ID')),
],
),
]
7 changes: 7 additions & 0 deletions tests/core/models.py
Expand Up @@ -97,6 +97,13 @@ class Meta:
app_label = 'core'


class BigAutoNowModel(models.Model):
id = models.BigAutoField(primary_key=True)

class Meta:
app_label = 'core'


class Counter(models.Model):
name = models.CharField(max_length=30)
slug = models.SlugField(unique=True)
Expand Down
29 changes: 28 additions & 1 deletion tests/core/tests/resources.py
Expand Up @@ -49,7 +49,7 @@

from core.models import (
Note, NoteWithEditor, Subject, MediaBit, AutoNowNote, DateRecord, Counter,
MyDefaultPKModel, MyUUIDModel, MyRelatedUUIDModel,
MyDefaultPKModel, MyUUIDModel, MyRelatedUUIDModel, BigAutoNowModel,
)
from core.tests.mocks import MockRequest
from core.utils import adjust_schema, SimpleHandler
Expand Down Expand Up @@ -1056,6 +1056,19 @@ def get_resource_uri(self, bundle_or_obj=None, url_name='api_dispatch_list'):
return '/api/v1/autonownotes/%s/' % bundle_or_obj.obj.id


class BigAutoNowModelResource(ModelResource):
class Meta:
resource_name = 'bigautonowmodels'
queryset = BigAutoNowModel.objects.all()
authorization = Authorization()

def get_resource_uri(self, bundle_or_obj=None, url_name='api_dispatch_list'):
if bundle_or_obj is None:
return '/api/v1/bigautonowmodels/'

return '/api/v1/bigautonowmodels/%s/' % bundle_or_obj.obj.id


class CustomPaginator(Paginator):
def page(self):
data = super(CustomPaginator, self).page()
Expand Down Expand Up @@ -1661,6 +1674,20 @@ def test_fields(self):
self.assertEqual(annr.fields['updated'].readonly, False)
self.assertEqual(annr.fields['updated'].unique, False)

def test_big_auto_field(self):
annr = BigAutoNowModelResource()
self.assertEqual(len(annr.fields), 2)
self.assertEqual(sorted(annr.fields.keys()), ['id', 'resource_uri'])

self.assertTrue(isinstance(annr.fields['id'], fields.IntegerField))
self.assertEqual(annr.fields['id'].attribute, 'id')
self.assertEqual(annr.fields['id'].blank, True)
self.assertEqual(annr.fields['id']._default, '')
self.assertEqual(annr.fields['id'].instance_name, 'id')
self.assertEqual(annr.fields['id'].null, False)
self.assertEqual(annr.fields['id'].readonly, False)
self.assertEqual(annr.fields['id'].unique, True)

def test_invalid_model_resource(self):
"""
Test error message regarding ModelResource lacking object_class and queryset.
Expand Down

0 comments on commit ac18543

Please sign in to comment.