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

Unable to add new administration record #8

Closed
dgets opened this issue Sep 20, 2018 · 11 comments
Closed

Unable to add new administration record #8

dgets opened this issue Sep 20, 2018 · 11 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@dgets
Copy link
Owner

dgets commented Sep 20, 2018

Unable to save to the database for an unknown reason. Such a very helpful error message. I should probably start looking for specific exceptions. Haven't had a chance to do that in python just yet. Anyway, the saving & adding record logic in the recadm app should be just about completed, as soon as this stumbling block is out of the way.

@dgets dgets added the bug Something isn't working label Sep 20, 2018
@dgets dgets added this to the alpha milestone Sep 20, 2018
@dgets dgets self-assigned this Sep 20, 2018
@dgets
Copy link
Owner Author

dgets commented Sep 20, 2018

Ah, learning about the exceptions was helpful:

int() argument must be a string, a bytes-like object or a number, not 'dict'

@dgets
Copy link
Owner Author

dgets commented Sep 20, 2018

Dumping the value of request.POST['dosage'] in the template shows that the correct value is ending up in the POST data, at least. Yet when the administration record is populated with the POST data, the dosage field is ending up None. Going to try manually parsing that as an integer for the model field.

@dgets
Copy link
Owner Author

dgets commented Sep 20, 2018

No workee.

@dgets
Copy link
Owner Author

dgets commented Oct 11, 2018

Okay, so to sum up what I think is going on with this issue for @georgedorn & @deutrino...

In this particular app, which only exists (I think?) in the feature/record-usage branch for now, you can see at line 7 in recadm/models.py the entry that I believe is responsible for the issue.

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

I'm utilizing the foreign key (from a model in subadd/models.py) to reference the substance administered for a particular entry, as is probably pretty self evident. Everything is parsing just fine, but the runtime error that was referenced above is my sole clue at this point.

I was going to include more information on this, but honestly, since I've let the project sit for a few weeks (not being able to turn up more information on this myself, at least up to this point) I've kind of forgotten what other information I had that might be helpful. Feel free to ping me on any supporting details that might be necessary.

@dgets
Copy link
Owner Author

dgets commented Oct 11, 2018

The value that I believe is the integer in question can be found here:

for sub in substances:

mydata.append({'name': sub.common_name, 'id': sub.id, })

Suddenly I have the feeling that I'm missing something basic enough that I should know it off the top of my head. Maybe taking a break from coding on this for a bit was a good thing, I think I have a thought brewing that may help this... Also, I am sure that there has to be a way to quote multiple lines from one of the source files in question, instead of just that link to one. If anybody can slap me with how to do this I'd be most appreciative.

@georgedorn & @deutrino

@georgedorn
Copy link

Yeah, getting access to the actual error message, preferably in traceback form, is going to make debugging this trivial.

You might try setting up the django-extensions app and turning on DEBUG, which should make the traceback appear in the browser, along with the ability to interact with the interpreter at any point in the traceback to find the values of local variables. It's going to make every debugging effort a great deal easier.

@dgets
Copy link
Owner Author

dgets commented Oct 13, 2018

Thank you for the input. I'm installing it now. I was figuring there must be some sort of bit I was missing to get a little more detailed information on all of this...

@dgets
Copy link
Owner Author

dgets commented Oct 13, 2018

Here's what I'm getting at this point:

October 13, 2018 - 13:54:51
Django version 2.2.dev20180928135712, using settings 'lasttime.settings'
Starting development server at http://192.168.0.2:8000/
Quit the server with CONTROL-C.
Internal Server Error: /recadm/save_admin
Traceback (most recent call last):
  File "/usr/local/src/django-git/django/utils/datastructures.py", line 77, in __getitem__
    list_ = super().__getitem__(key)
KeyError: 'substance'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/src/django-git/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/usr/local/src/django-git/django/core/handlers/base.py", line 127, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/src/django-git/django/core/handlers/base.py", line 125, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/sprite/src/py/django/lasttime/recadm/views.py", line 45, in save_admin
    administration = Usage({'sub': request.POST['substance'],
  File "/usr/local/src/django-git/django/utils/datastructures.py", line 79, in __getitem__
    raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'substance'
[13/Oct/2018 13:54:56] "GET /recadm/save_admin HTTP/1.1" 500 78489
Not Found: /favicon.ico
[13/Oct/2018 13:54:56] "GET /favicon.ico HTTP/1.1" 404 2190
[13/Oct/2018 13:55:03] "GET /recadm/add HTTP/1.1" 200 920
[13/Oct/2018 13:55:12] "POST /recadm/save_admin HTTP/1.1" 200 1061

I guess it's the list_ = super().__getitem__(key) bit that's throwing me. That's something bogus happening when it's trying to access the ForeignKey, I think, but I'm not completely sure on that, and not sure where to go next. :|

@georgedorn
Copy link

list_ = super().__getitem__(key) is just django 2.2 wrapping the standard dict object. The real error is in the second traceback (whenever you get two, one is way more important than the other).

What it is saying is that you are looking for request.POST['substance'] and it doesn't exist. That probably means the browser never sent it, for whatever reason, either because that field isn't in a form tag that was submitted or whatever. There's some useful ways of dealing with this, though. One is to provide a default value instead of using it like a dict: request.POST.get('substance', 'default_here') won't throw a KeyError if 'substance' is missing.

But way more likely, you don't want to do any of this, and instead should be using a django form for this.

@dgets
Copy link
Owner Author

dgets commented Oct 13, 2018

I actually switched to using forms just a little bit after this problem roadblocked me, or else right before. I didn't know how to do the ForeignKeys` retrieval for the radio button menu on the form yet, or else I would've implemented it that way. That shouldn't be too tough to find, though, I'll see if I can't find that, since I'm already using forms in one other area.

dgets added a commit that referenced this issue Oct 14, 2018
… to try doing it with the model, then had the obvious pointed out that it was time to switch to Django forms. In the process of implementing that; no testing has taken place yet, tho.
@dgets
Copy link
Owner Author

dgets commented Oct 14, 2018

Alright so, in the last 2 commits, I began implementing things the Django Way(tm), instead of with the horrendous workarounds that I had used before I hit those portions of the tutorials. Switching over seemed easy enough, except for the ForeignKey bit. See also line 5:

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

Any suggestions as to where I could go for some more information on this particular bit of the issue? I've not been able to turn up anything yet.

BTW: @georgedorn, your pointers and explanations are very much appreciated, just so you know. :) Thank you for taking some time to point out the trivial & obvious to me here. ;)

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