Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not sure what to use instead of ForeignKey in the recadm/add app's forms.py #11

Closed
dgets opened this issue Oct 14, 2018 · 2 comments
Closed
Assignees
Labels
bug Something isn't working
Milestone

Comments

@dgets
Copy link
Owner

dgets commented Oct 14, 2018

sub = forms.ForeignKey('subadd.Substance', on_delete=models.CASCADE)

  File "/home/sprite/src/py/django/lasttime/recadm/urls.py", line 3, in <module>
    from . import views
  File "/home/sprite/src/py/django/lasttime/recadm/views.py", line 5, in <module>
    from .forms import UsageForm
  File "/home/sprite/src/py/django/lasttime/recadm/forms.py", line 4, in <module>
    class UsageForm(forms.Form):
  File "/home/sprite/src/py/django/lasttime/recadm/forms.py", line 5, in UsageForm
    sub = forms.ForeignKey('subadd.Substance', on_delete=models.CASCADE)
AttributeError: module 'django.forms' has no attribute 'ForeignKey'

No workee; having trouble finding a concise resource on what I should be looking at instead of ForeignKey here... It may well help things out a little bit to check into the validity of on_delete=models.CASCADE in forms.py, as well. Somehow I think that if it actually is necessary, it'll at least look a little different-- say an s/models/forms/, if nothing else...

This came about while trying to implement a solution for #8.

@dgets dgets added the bug Something isn't working label Oct 14, 2018
@dgets dgets added this to the alpha milestone Oct 14, 2018
@dgets dgets self-assigned this Oct 14, 2018
@georgedorn
Copy link

There's a couple options here:

Try making this a forms.ModelForm - see https://docs.djangoproject.com/en/2.1/topics/forms/modelforms/ - then you really don't need to specify much at all, except the model you are basing the form on, and anything custom you want to add. It looks like, from the docs, that a models.ForeignKey in the model becomes forms.ModelChoiceField which defaults to a select drop-down widget.

Alternately, you can make sub a ChoiceField or a ModelChoiceField. I'm less clear on how that would work, you'd have to do more heavy lifting yourself.

@dgets
Copy link
Owner Author

dgets commented Oct 15, 2018

Okay, I've got the majority of the initial changes made, I think. Unfortunately, I'm running into the error with trying to use the model from a different app, within the same project, again; referencing it from the query and/or from imports in the same way, working previously in the form, prior to the ModelForm usage is no longer workee. :| Going to open a separate issue for that, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants