Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow non-integer primary keys #46

Open
thenewguy opened this issue May 6, 2024 · 2 comments
Open

Allow non-integer primary keys #46

thenewguy opened this issue May 6, 2024 · 2 comments

Comments

@thenewguy
Copy link

The basket serializer expects an Integer primary key for product_id.

From an initial search it looks like this is the only spot requiring Integer primary keys. UUID primary keys are common and for consistency it is nice to use them here.

Please consider adding support for UUID (or more generically, non-integer) primary keys.

Thanks!

@thenewguy
Copy link
Author

Also - I would be happy to provide PRs for the issues that I have raised. Please let me know if you will accept suitable PRs for any of them. Thanks!

@thenewguy
Copy link
Author

thenewguy commented May 6, 2024

I worked through this. The required changes would be:

  • modify salesman.basket.serializers.BasketItemCreateSerializer.product_id to be a CharField or allow the BasketItemCreateSerializer to be swapped. Currently this one has to be monkey patched

The generally useful changes would be:

  • modifying BasketItem.product_id and OrderItem.product_id to use a CharField so the generic relation works with IntegerFields or UUIDFields out of the box. Currently, this one can be overridden on the swapped models.

The documentation changes would be:

  • cast the basket id in PaymentMethod.basket_payment like order.extra["basket_id"] = str(basket.id)
  • unless basket id stored on session is cast to str, depending on the deployment, may also need to provide a session serializer with settings.SESSION_SERIALIZER like:

from rest_framework.utils.encoders import JSONEncoder


class JSONSerializer:
    """
    Simple wrapper around json to be used in signing.dumps and
    signing.loads.
    """
    def dumps(self, obj):
        return json.dumps(obj, separators=(',', ':'), cls=JSONEncoder).encode('latin-1')

    def loads(self, data):
        return json.loads(data.decode('latin-1'), cls=JSONEncoder)

So far those seem to be the only required changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant