Wednesday, January 27, 2021

Re: Show FK as input instead of drop down in admin

On 28/01/2021 3:28 pm, Jim Illback wrote:
> I think Kevin's issue is that it takes a huge amount of runtime to create the FK's select HTML tag - Django having to go through 1M rows before displaying the page. This issue is true in UpdateView CBVs as well.
>
> Changing the situation slightly - I'm talking about just primary keys from here on, not all FK's. Is there some justification for Django forcing UpdateViews (and admin pages) to have to have the entire set of primary keys displayed and able to be changed? I believe the real world doesn't operate like that. In SAP or Oracle apps or any other business process, you can't change the primary key value willy nilly. For example, should an admin at Amazon be able to take my prime order and re-assign it to some other random person? No.
>
> And, even if this is a specific business requirement (for example, one subsidiary needs their purchase order transferred to their peer subsidiary), that should be done via a unique exception process that checks all the business rules and circumstances, such as ensuring both companies belong to the same corporation. However, those are exception transactions, not the forced norm.
>
> To make this situation work today in Django updates, all applications using an update with a primary key to a large table has to write special form or view __init__() methods to limit primary key or keys shown. Otherwise, you have to wait for the page to build enormous primary key select tags. Why?

Surely that is because Django cannot know what the developer wants. For
that reason, Django suggests to write your own get_queryset() and your
own formfield_for_foreignkey() methods in the admin to fetch relevant
data from the database.

In my response to Kevin it was formfield_for_foreignkey() that I was
alluding to in my option #1.

I realise that you have moved on from the Admin in your response above,
but the principle is the same. I should not retrieve more than I need
and I'm the only one who knows what I need.

Cheers

Mike

>
> Jim
>
>> On Jan 27, 2021, at 7:07 PM, Mike Dewhirst <miked@dewhirst.com.au> wrote:
>>
>> On 28/01/2021 12:33 am, Kevin Olbrich wrote:
>>> Hi!
>>>
>>> Is it possible to disable fetching of related models for admin pages?
>>> I have a field that links to a table containing more than 1M rows which django tries to fetch and build the drop down. For my purpose, it is sufficient if it is a simple input containing the id instead.
>> Couple of options ...
>>
>> 1. If there is a subset of 1M rows which apply you can filter the drop down list to just a few - or
>>
>> 2. Avoid the issue altogether
>>
>> - make the fk field in the model null=True and blank=True
>>
>> - make the fk field in the admin readonly
>>
>> - add a new integer field for the user-entered value of the desired id
>>
>> - in the model save() method[1] find the desired instance from the 1M rows and put it into the real fk field
>>
>> Might work but not tested. This is a thought experiment.
>>
>> Mike
>>
>> [1]
>> def save(self, *args, **kwargs):
>> # might need some adjusting for safety, business rules etc
>> self.onem = OneM.objects.get(id=self.pseudo_fk)
>> super().save(*args, **kwargs)
>>
>>> How can I accomplish this?
>>>
>>> Thank you!
>>>
>>> Kind regards
>>> Kevin
>>> --
>>> You received this message because you are subscribed to the Google Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com <mailto:django-users+unsubscribe@googlegroups.com>.
>>> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/12a7c928-a960-4c48-b631-93153260adbfn%40googlegroups.com <https://groups.google.com/d/msgid/django-users/12a7c928-a960-4c48-b631-93153260adbfn%40googlegroups.com?utm_medium=email&utm_source=footer>.
>>
>> --
>> Signed email is an absolute defence against phishing. This email has
>> been signed with my private key. If you import my public key you can
>> automatically decrypt my signature and be sure it came from me. Just
>> ask and I'll send it to you. Your email software can handle signing.
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
>> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/24a0de08-f460-5dea-47d4-df77a5d6170d%40dewhirst.com.au.


--
Signed email is an absolute defence against phishing. This email has
been signed with my private key. If you import my public key you can
automatically decrypt my signature and be sure it came from me. Just
ask and I'll send it to you. Your email software can handle signing.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e2404f91-67b7-2b2e-f5a1-72d03a097cc8%40dewhirst.com.au.

No comments:

Post a Comment