Wednesday, February 28, 2018

Re: Is there a way to make a field both foreignKey or allow user to fill it in themself?



On Wed, Feb 28, 2018 at 12:46 AM, Mike Dewhirst <miked@dewhirst.com.au> wrote:
On 28/02/2018 1:58 PM, Alexander Joseph wrote:
Sorry, I think my question was confusing. What I want to do is allow the user to either select a user from a drop down list of users from the ForeignKey in the Users model, or if there is not a user suitable to select in the drop down, they can manually type in a name themselves.

In the same field that would require a hack of the first water.  Way above my pay grade. For me, anything which equates an existing user object with a string typed in is likely to end in misery.

Otherwise a separate CharField for the typed in name and null=True and blank=True in the ForeignKey ought to work.

Mike

Indeed, what you can also do - besides the nullable FK, is process that in the Form where if the FK is null and the the charfield is not None, then create a User (maybe non active) with that name and assign it to the FK. 
 


Thanks for your reply



On Tuesday, February 27, 2018 at 7:45:51 PM UTC-7, Mike Dewhirst wrote:

    On 28/02/2018 1:26 AM, Alexander Joseph wrote:
    > Is there a way to make a form field or model field either a foreign
    > key reference or allow the user to fill in something themselves?
    I'm
    > making a fixed assets app and each fixed asset has an owner field,
    > which I would like to assign to an app user if possible, and if not
    > possible be able to fill in a name myself.

    Yes. Make the ForeignKey in the model null=True and blank=True so
    it can
    exist all alone. Then you can limit_choices_to whatever whatever
    suits
    your requirements ...

    https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.ForeignKey.limit_choices_to
    <https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.ForeignKey.limit_choices_to>


    hth
    Mike


    >
    > Thanks
    > --
    > 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...@googlegroups.com <javascript:>
    > <mailto:django-users+unsubscribe@googlegroups.com <javascript:>>.
    > To post to this group, send email to django...@googlegroups.com
    <javascript:>
    > <mailto:django...@googlegroups.com <javascript:>>.
    > Visit this group at https://groups.google.com/group/django-users
    <https://groups.google.com/group/django-users>.
    > To view this discussion on the web visit
    >
    https://groups.google.com/d/msgid/django-users/98c44317-0993-4d41-9d25-33f8a9821dce%40googlegroups.com
    <https://groups.google.com/d/msgid/django-users/98c44317-0993-4d41-9d25-33f8a9821dce%40googlegroups.com>

    >
    <https://groups.google.com/d/msgid/django-users/98c44317-0993-4d41-9d25-33f8a9821dce%40googlegroups.com?utm_medium=email&utm_source=footer
    <https://groups.google.com/d/msgid/django-users/98c44317-0993-4d41-9d25-33f8a9821dce%40googlegroups.com?utm_medium=email&utm_source=footer>>.

    > For more options, visit https://groups.google.com/d/optout
    <https://groups.google.com/d/optout>.

--
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 post to this group, send email to django-users@googlegroups.com <mailto:django-users@googlegroups.com>.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/dd78c8cf-1892-4740-b3cb-1e384b9bd788%40googlegroups.com <https://groups.google.com/d/msgid/django-users/dd78c8cf-1892-4740-b3cb-1e384b9bd788%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

--
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4facfe44-e50e-e7d4-e41a-1c82775a8f7f%40dewhirst.com.au.

For more options, visit https://groups.google.com/d/optout.

--
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BFDnhJbavWjzyOnBLaTF1-VZTsHXjsjcfpWBEmaMyUsre--hA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment