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

27 views
Skip to first unread message

Alexander Joseph

unread,
Feb 27, 2018, 9:26:22 AM2/27/18
to Django users
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.

Thanks

Mike Dewhirst

unread,
Feb 27, 2018, 9:45:51 PM2/27/18
to django-users@googlegroups.com >> Django users
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

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
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@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/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>.
> For more options, visit https://groups.google.com/d/optout.

Alexander Joseph

unread,
Feb 27, 2018, 9:58:29 PM2/27/18
to Django users
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.

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

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

Mike Dewhirst

unread,
Feb 27, 2018, 10:47:21 PM2/27/18
to django...@googlegroups.com
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

>
> 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...@googlegroups.com <javascript:>>.
> > To post to this group, send email to django...@googlegroups.com
> <javascript:>
> > <mailto:django...@googlegroups.com <javascript:>>.
> <https://groups.google.com/group/django-users>.
> <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/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...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@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>.

Matemática A3K

unread,
Feb 28, 2018, 11:21:20 PM2/28/18
to django...@googlegroups.com
On Wed, Feb 28, 2018 at 12:46 AM, Mike Dewhirst <mi...@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. 
 

    > <mailto:django-users+unsubscrib...@googlegroups.com <javascript:>>.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com <mailto:django-users+unsubscrib...@googlegroups.com>.
To post to this group, send email to django...@googlegroups.com <mailto:django-users@googlegroups.com>.
--
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...@googlegroups.com.

Mike Dewhirst

unread,
Mar 1, 2018, 12:16:58 AM3/1/18
to django...@googlegroups.com
On 1/03/2018 3:20 PM, Matemática A3K wrote:
>
>
> On Wed, Feb 28, 2018 at 12:46 AM, Mike Dewhirst <mi...@dewhirst.com.au
> <mailto:mi...@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.

It will depend on the use-case requirements but I think that could get
slightly messy. You need to ensure you are not creating duplicate
people. For example you would likely need case-insensitive names.
> <mailto:django-users...@googlegroups.com> <javascript:>
>     > <mailto:django-users...@googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>
> <javascript:>>.
>     > To post to this group, send email to
> django...@googlegroups.com <mailto:django...@googlegroups.com>
>     <javascript:>
>     > <mailto:django...@googlegroups.com
> <mailto:django...@googlegroups.com> <javascript:>>.
>     > Visit this group at
> https://groups.google.com/group/django-users
> <https://groups.google.com/group/django-users>
>     <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
> <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>
>    
> <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>
>     <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...@googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>
> <mailto:django-users...@googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>>.
> To post to this group, send email to
> django...@googlegroups.com
> <mailto:django...@googlegroups.com>
> <mailto:django...@googlegroups.com
> <mailto:django...@googlegroups.com>>.
> 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/dd78c8cf-1892-4740-b3cb-1e384b9bd788%40googlegroups.com
> <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
> <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
> <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...@googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> 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/4facfe44-e50e-e7d4-e41a-1c82775a8f7f%40dewhirst.com.au
> <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
> <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...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@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
> <https://groups.google.com/d/msgid/django-users/CA%2BFDnhJbavWjzyOnBLaTF1-VZTsHXjsjcfpWBEmaMyUsre--hA%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Alexander Joseph

unread,
Mar 1, 2018, 9:56:56 AM3/1/18
to Django users
I think the solution is above my head at the moment. I'll have to look back into it once I get better at Django. Thanks for your help though
Reply all
Reply to author
Forward
0 new messages