Django: hiding the primary key (pk) and using DetailView with get_queryset -
following on thread: django: how hash url database object's primary key?, hide primary keys users , use detailview
in urlconf. able accomplish hiding using bitwise xor part (it worked in view functions), until got part had "unmask" masked primary key sent in url subclass of detailview
.
how "unmask" pk_masked named group before sending detailviewfilteredonuser(detailview)
instance? there way send mask_toggle(pk_unmasked)
call detailviewfilteredonuser right in urlconf? in searching solutions, found pk_url_kward
in django documentation, couldn't work , anyways don't think can me perform operations on primary key detailview operates on.
here masking function:
def mask_toggle(number_to_mask_or_unmask): return int(number_to_mask_or_unmask) ^ settings.masking_key
my models "pkgs" contain "items":
class pkg(models.model): user = models.foreignkey(user, editable=false) tracking_number = models.charfield(max_length=60, unique=true) class item(models.model): pkg = models.foreignkey(pkg) description = models.charfield(max_length=300)
here in urls.py:
class listviewfilteredonuser(listview): def get_queryset(self): return pkg.objects.order_by('-created_at').filter(user=self.request.user) class detailviewfilteredonuser(detailview): def get_queryset(self): qs = super(detailviewfilteredonuser, self).get_queryset() return qs.filter(user=self.request.user) .... url(r'^(?p<pk_masked>\d+)/$', login_required(detailviewfilteredonuser.as_view( model=pkg, template_name='pkgs/detail.html' )), name='detail'),
so problem if named group in urlconf "pk", masked primary key (because masked key in url) sent detailview. if named group in urlconf "pk_masked", need pk=mask_toggle(pk_masked)
somewhere, , can't figure out or how this. thanks.
if understood problem correctly, need override get_object
(which makes get_queryset
pretty irrelevant, can still use clarity). like:
class detailviewfilteredonuser(detailview): model = pkg template_name = 'pkgs/detail.html' def get_queryset(self): return super(detailviewfilteredonuser, self).get_queryset().filter(user=self.request.user) def get_object(self): return self.get_queryset().get(pk=mask_toggle(self.kwargs.get("pk_masked"))
(of course, don't forget catch exceptions, left out clarity , brevity.)