Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class Dog(models.Model):
|**prefix**|`str` |❌|`""`|If provided, the ID strings generated as the field's default value will be prefixed. This provides a way to have a per-model prefix which can be helpful in providing a global namespace for your ID system. The prefix should be provided as a string literal (e.g `cus_`). For more, see below.|
|**max_length**|`int`|✅|Set it|Controls the maximum length of the stored strings. Provide your own to match whatever ID system you pick, remembering to take into account the length of any prefixes you have configured. Also note that there is no perf/storage impact for modern Postgres so for that backend it is effectively an arbitary char limit.|
|**primary_key**|`boolean`|❌|`False`|Set to `True` to replace Django's default `Autofield` that gets used as the primary key, else the field will be additional ID field available to the model.|
|**unique**|`boolean`|❌|`True`|Whether the field should be treated as unique across the dataset; the field provides a sane default of `True` so that a database index is setup to protext you against collisions (whether due to chance or, more likely, a bug/human error). To turn the index off, simply pass `False`.|
|**unique**|`boolean`|❌|`False`|Whether the field should be treated as unique across the dataset; when `primary_key=True`, unique is implied. Behave the same as in `django.do.models.fields.CharField`.

All other `django.db.models.fields.CharField` keyword arguments should work as expected. See the [Django docs](https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.CharField).

Expand Down
4 changes: 0 additions & 4 deletions charidfield/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ def __init__(
else:
kwargs["default"] = default

# Ensure a unique index is set up by default unless the caller
# explicitly disables it; this seems like the sane thing to do.
kwargs.setdefault("unique", True)

super().__init__(*args, **kwargs)

def get_internal_type(self) -> str:
Expand Down
3 changes: 3 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class IDModel(models.Model):
# Showcase the default field but without an index.
no_index_id = TestUIDField(unique=False)

# Showcase setting unique explicitly.
unique_id = TestUIDField(unique=True)

# Showcase with a static default + a prefix. This is very much an edge
# case as I can't think of a good reason to ever want a static default
# for an ID field, but given you can call it like this: we should test it.
Expand Down
20 changes: 20 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ def test_setting_primary_key(self):
# object will not persist.
self.instance_a.default_id = generate_test_uid()
self.instance_a.prefixed_id = generate_test_uid(prefix="dev_")
self.instance_a.unique_id = generate_test_uid()
self.instance_a.save()
self.instance_a.refresh_from_db()
assert self.instance_a.id == new_id
Expand Down Expand Up @@ -244,6 +245,23 @@ def test_setting_non_primary_key(self, model_field_name):
if old_id:
assert IDModel.objects.filter(**{model_field_name: old_id}).count() == 0

@pytest.mark.parametrize(
"model_field_name, expected",
(
("id", True),
("unique_id", True),
("default_id", False),
("prefixed_id", False),
("null_id_with_no_default", False),
("not_null_id_but_blank", False),
("no_index_id", False),
("prefixed_and_non_callable_default_id", False),
),
)
def test_unique_set_default(self, model_field_name, expected):
field = self.instance_a._meta.get_field(model_field_name)
assert field.unique is expected

def test_foreign_key__with_parent_model__instance(self):
related_instance = RelatedIDModel.objects.create(
name="Blue Album", parent=self.instance_a
Expand Down Expand Up @@ -277,6 +295,7 @@ def test_dumpdata(self):
"null_id_with_no_default": None,
"not_null_id_but_blank": "",
"no_index_id": self.instance_a.no_index_id,
"unique_id": self.instance_a.unique_id,
"prefixed_and_non_callable_default_id": "test_abcde",
},
},
Expand All @@ -290,6 +309,7 @@ def test_dumpdata(self):
"null_id_with_no_default": None,
"not_null_id_but_blank": "",
"no_index_id": self.instance_b.no_index_id,
"unique_id": self.instance_b.unique_id,
"prefixed_and_non_callable_default_id": "test_abcde",
},
},
Expand Down