That makes sense. If you have a patch suggestion we would be very grateful - otherwise feel free to open a bug on github so that we don't lose track of this :)
Thanks a lot
- Chris
Well perhaps the fact that it was "many times" makes it a prime
candidate for core :)
Ales, I think not messing around with django's IDs is a good idea - so
perhaps we should just add a perfect hash field to all order objects
and make the slug for Orders be this by default in core? Then people
can change that to whatever they see fit - but it seems this would
cover most bases?
- Chris
To save one of the database saves you could look at just generating a
random string. You would have to code for the expected id collision
though. You would also eventually need an index on invoice_id.
What does everyone think is the most common use case?
Keep in mind, that for instance, Austrian law requires consecutive numbers on their invoices, so random order identifiers or hashes are not an option.
- Jacob
I like the method on Order solution - so that we provide a sane
default but let people adapt the solution to their need.
As for the "random versus hash" bit it seems like random would work
perhaps a little more "cheaply" since collisions are unlikely to
happen very often if the field is large enough, but a perfect hash
would be equally suitable.
- Chris
If we want to support on the fly hash adjustments we need two additional methods.
One method that prepares the hash for display to the user
and another that prepares the hash for database queries.
However, I wrote a proof of concept the other day that generates a random value and handles collisions within the Field class so that Model definitions do not need to be altered.I am going to rewrite it cleanly so that one base class can be used generically for any random field regardless of the random value being an integer, string, binary data, etc.
Hi gordon, I had a quick look and it seems plenty complicated, however, it does also seem to cover most cases.
My opinion is that we definitely need an order_number as CharField, not sure what the default method for generating it should be though. Probably keeping it simple would be best, while at the same time allowing the use of something like the app you proposed.
My opinion is that we definitely need an order_number as CharField, not sure what the default method for generating it should be though. Probably keeping it simple would be best, while at the same time allowing the use of something like the app you proposed.
I think by default, shop should just use an autofield and make it simple to override the "id" field class via settings for all shop models. There were too many opinions about the requirements of the default primary key to make a choice that will satisfy everyone.
Too complicate and not flexible enough. The Order class can be reconfigured anyway.I would add a Slug-Field to the Order model. Why Slug? Because a) it must be unique anyway, b) Slug is a well established term in Django and c) when displaying the Orders Detail View, this Slug then simply is the part of the URL.To the default Order class, we shall add as the Slug generator, a function which simply returns the current primary key. This keeps things simple and backwards compatible.Everyone who wishes to use a different Order numbering schema, just has to derive from the current Order class and overload this one function.- Jacob
IMO the common case is an auto-incrementing id. Maybe just because
most are not bothered enough to choose something else! If that is
going to be the default it seems redundant to also create a charfield
+ unique index just to cater for the few who need something more
exotic. The second most common IMO is an auto incrementing id starting
at an offset.
We could look at making it easier to override without breaking
existing code. I was thinking about implement it similar to Model.pk
in Django. https://github.com/django/django/blob/master/django/db/models/base.py#L449
The problem comes when trying to get it to work with the ORM. The only
implementation I came up with involved overriding all the queryset
methods. All the code to make the pk attribute work as it does is
dotted throughout the db related code in Django. Hmm...
The changes you suggest set an attribute on Order that points to a
model field. Which is the same as what I linked to in Django source
code. But that is only half the story. The difficultly is getting that
property to behave like a model field in the orm. What happens if you
do: Order.objects.get(slug=123) ? My opinion is that that should work.
What do others think? I also don't think slug is the right name. My
suggestion would be `invoice_id` or `order_id`
Order.objects.get(**{Order.get_slug_field(): 123})
IMHO the more we keep to an existing common API the better.
Order.objects.get(pk=123)
Here is a quick hack to illustrate the concept:
https://github.com/simonluijk/django-shop/commits/orderid/
field name on the model. My preference would be "oi" for order identifier like "pk" is for primary key.
Wouldn't that prevent subclasses from changing the field type and requirements? Or do you mean by requiring a field named "order_foo" to be on the order?
https://docs.djangoproject.com/en/dev/topics/db/models/#field-name-hiding-is-not-permitted
Sorry for the top post. I am not around a computer and my phone did it by default.
I'm ok with that. It makes everything much simpler, and if you really want something special you can anyway overwrite the whole model and views.