Changes between Initial Version and Version 1 of Ticket #30386, comment 6


Ignore:
Timestamp:
Oct 7, 2021, 8:28:53 AM (3 years ago)
Author:
Alexander Pervakov

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #30386, comment 6

    initial v1  
    11I'm dive deeper into this and found that `quote(obj.id)` maybe not the best option and should be debated but the presence of a warning message when `not isinstance(obj.pk, int)` would be great and will lead people to this thread. Because stackoverflow is full of questions about quoting the link in admin but no one provide refs to this ticket that I found after couple hours of code investigating and testing.
     2
     3Something like this django.db.models.fields.related.ForeignKey:
     4
     5
     6{{{
     7class ForeignKey(ForeignObject):
     8
     9    def check(self, **kwargs):
     10        return [
     11            *super().check(**kwargs),
     12            *self._check_on_delete(),
     13            *self._check_unique(),
     14            *self._check_pk_int(),
     15        ]
     16
     17    def _check_pk_int(self):
     18        remote_pk = getattr(self.remote_field, 'pk', None)
     19        if isinstance(remote_pk, int):
     20            return []
     21        else:
     22            return [
     23                checks.Warning(
     24                    'Not integer primary key may lead you to unpredictable url in admin when ForeignKeyRawIdWidget is used',
     25                    hint='Use extra quote() in this relation',
     26                    obj=self,
     27                    id='fields.W346',
     28                )
     29            ]
     30}}}
Back to Top