Set default value in field dependant on another model?

16 views
Skip to first unread message

Lachlan Musicman

unread,
Jan 14, 2019, 12:06:33 AM1/14/19
to django...@googlegroups.com
We have two models: Resources and Quota

Resources model instances include:
- red things
- yellow things 
- blue things

Quota have two fields, one is a FK to a Resource, the other is "desired quota".
When instantiating the Quota request form, we create a form for each Resource.

I'd like to autofill the "desired quota" field on each form, but make it dependent on which Resource it is representing.

IE, when someone makes a resource request, we expect them to want

1 red thing
5 yellow things
10 blue things

Do some searching, there's a lot pointing to Signals, but I remember someone once saying "if you are using signals, you should re-write you logic" or somesuch, so I thought I'd ask here.

FWIW - and this is well beyond my control - we are on Django 1.8

Cheers
L.

--
------
"The fact that you're still saying something like 'feminism gone mad' suggests that feminism hasn't gone mad ENOUGH yet."

Helen Zaltzman / @HelenZaltzman
https://twitter.com/HelenZaltzman/status/1065384934846545920

Michal Petrucha

unread,
Jan 14, 2019, 12:06:29 PM1/14/19
to django...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

On Mon, Jan 14, 2019 at 04:05:27PM +1100, Lachlan Musicman wrote:
> We have two models: Resources and Quota
>
> Resources model instances include:
> - red things
> - yellow things
> - blue things
>
> Quota have two fields, one is a FK to a Resource, the other is "desired
> quota".
> When instantiating the Quota request form, we create a form for each
> Resource.
>
> I'd like to autofill the "desired quota" field on each form, but make it
> dependent on which Resource it is representing.
>
> IE, when someone makes a resource request, we expect them to want
>
> 1 red thing
> 5 yellow things
> 10 blue things
>
> Do some searching, there's a lot pointing to Signals, but I remember
> someone once saying "if you are using signals, you should re-write you
> logic" or somesuch, so I thought I'd ask here.
>
> FWIW - and this is well beyond my control - we are on Django 1.8

Hi Lachlan,

This sounds like logic that would be triggered by the view handling
the form – when you're displaying a new empty form, ask your business
logic layer for the default number of things to request in a new
“Quota” for that specific resource, and then pass that number to your
form as the “initial” argument.

Cheers,

Michal
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAEBCgAGBQJcPMF3AAoJEHA7T/IPM/kl7aMQAKxO1sPQptRYCDYFzh/7UVLL
pBxxHT258Bnd3Oos73U05HQX7cij2Ca0aiJaFFK3nhd8SRl+WAKrzyNT40JBaEGC
dXXZrVxU4aUcHWsUKmGsbEXDEejnyGUM2sNU3rVzfs7zBdJl69fHKAFCeRc+iCJ8
ckWb/f0QCzqxEu5mVDC6UVqxWZY5QNws2bJ9zIeWVQZHaAhIeJrmrklKEEbstodi
9QGh64NHNZSn9kgNHiBYP39TKaTxa8QTN0pojB/U0VPt46AUx3Dyq/03GwK/8+mN
F4eUViiTntTCFsY0Py6jGh9/Oc8Yx0dRaQfalCj2uq6BSMLzIG+aZ8ubhkLq3DkK
6WvJ/nhhVNRyFjHFjV5lk7R5V/2mOPBFzIuPeWhUDFqYX8RA+SIZyqdZ9iabTxIJ
JG5Cp5FYZ0ToteQq8hWZcE9e/ZoB6c/b3aJwQ8MIqcCp8Rmh3cyjVFa1z3r3UJmG
j73T15NMwNk9b7g91sfluqhBWtPL31mH/7qTDtNXYC6WWawqCiol7y/Yu0xn2iws
xmPUYM4CDkF257t8KJxLzU69qhcB0xtRaqD5F31ioM4S+3WfCpXXmYUk7kPUUDzP
7KMr6s4sMq7BtdVxP4P3NEgfnfmyPkhiFuGX+v9OqV2VdbGTe7UkLuwCeXlD5zBb
cY+/1Z/YnrNn+EaspENW
=bT81
-----END PGP SIGNATURE-----
Reply all
Reply to author
Forward
0 new messages