Wednesday, January 27, 2021

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

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.

No comments:

Post a Comment