Hi I would like to create a custom url in my django admin.
The default URL when editing an object is.
http://localhost:8000/admin/cart/cart_id/change
In my admin
http://localhost:8000/admin/cart/1/change
I have a field called cart unique id. I want to create a custom url that behaves similar to the edit URL in django admin.
http://localhost:8000/admin/cart/uniq_id/change
http://localhost:8000/admin/cart/H2KPAT/change
Is this implemation possible?
Iain Shelvington :
By default the admin will use the primary key of your model for the admin urls, you could set this unique field as the primary key of the model to achieve this.\n\nyour_field = models.TypeOfField(primary_key=True)\n\n\nIf you don't want to do this you could override the get_object method of your model admin\n\ndef get_object(self, request, object_id, from_field=None):\n queryset = self.get_queryset(request)\n model = queryset.model\n # This would usually default to the models pk\n field = model._meta.get_field('you_field') if from_field is None else model._meta.get_field(from_field)\n try:\n object_id = field.to_python(object_id)\n return queryset.get(**{field.name: object_id})\n except (model.DoesNotExist, ValidationError, ValueError):\n return None\n",
2019-07-09T03:56:37