is Django AdminForm missing has_changed? - django

Hi I'm very new to Django, and expect I'm missing something simple, so please bear with me.
I am encountering a 500 error when trying to save data on an admin page.
We are using grapelli-nested-inlines (https://github.com/datahub/grappelli-nested-inlines) on this page.
When I make the text change to a TextField, I receive this error:
AttributeError at /_admin/...
'InlineAdminFormSet' object has no attribute 'has_changed'
Request Method: POST
Request URL: http://localhost:8000/_admin/...
Django Version: 1.7.4
Exception Type: AttributeError
Exception Value:
'InlineAdminFormSet' object has no attribute 'has_changed'
Exception Location: venv/lib/python3.4/site-packages/grappelli_nested/forms.py in <genexpr>, line 36
Python Executable: venv/bin/python
Python Version: 3.4.3
Python Path:
['venv/lib/python34.zip',
'venv/lib/python3.4',
'venv/lib/python3.4/plat-darwin',
'venv/lib/python3.4/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4',
'/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin',
'venv/lib/python3.4/site-packages']
Server time: Thu, 13 Aug 2015 15:24:08 +0000
InlineAdminFormSet is defined in https://github.com/django/django/blob/master/django/contrib/admin/helpers.py as an AdminForm.
Based on the Django docs I would expect all forms to have the has_changed method defined, but this one doesn't seem to.
I don't see anyone else referencing this issue, so that leads me to believe I'm missing something.
Can someone please clarify for me:
Should AdminForm have a has_changed method (if so how do I go about pushing that in?)
if not should I just handle this in a fork of grapelli?
or is there some other solution I'm missing.
Cheers

AdminForm and InlineAdminFormSet are undocumented internals. They do not inherit from forms.Form, so they don't have a has_changed method.
You could try opening an issue for grappelli-nested-inlines, or dig deeper into the internals and try to figure it out.

Related

Django Expression contains mixed types: DateTimeField, IntegerField. You must set output_field

When I am trying to create new post from admin model, I m getting this error.
I search many errors like that in google and everywhere wrote that query expression(aggregate, annotation) causes this error, but the reason why I stucked on that error is, I am not using query expression, like aggregation and annotation
FieldError at /admin/blog_post/post/add/
Expression contains mixed types: DateTimeField, IntegerField. You must set output_field.
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/blog_post/post/add/
Django Version: 4.1.3
Exception Type: FieldError
Exception Value:
Expression contains mixed types: DateTimeField, IntegerField. You must set output_field.
I cant do anything because i am not using query expresison
can someone tell me where is the main reason and solution?

NoReverseMatch: with arguments '(u'',)' and keyword arguments '{}' not found.in Django Admin

I found a better error message (see below).
I have a model called App in core/models.py. The error occurs when trying to access a specific app object in django admin. Even on an empty database (after syncdb) with a single app object.
Seems core_app_history is something django generated. Any help is appreciated.
Here is the exception:
NoReverseMatch at /admin/core/app/251/
Reverse for 'core_app_history' with arguments '(u'',)' and keyword arguments '{}' not found.
Request Method: GET
Request URL: http://weblocal:8001/admin/core/app/251/
Django Version: 1.5.4
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'core_app_history' with arguments '(u'',)' and keyword arguments '{}' not found.
Exception Location: /opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/template/defaulttags.py in render, line 426
Python Executable: /opt/virtenvs/django_slice/bin/python
Python Version: 2.7.3
Python Path:
['/opt/src/slicephone/cloud',
'/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg',
'/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg',
'/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/distribute-0.6.35-py2.7.egg',
'/opt/virtenvs/django_slice/lib/python2.7',
'/opt/virtenvs/django_slice/lib/python2.7/plat-linux2',
'/opt/virtenvs/django_slice/lib/python2.7/lib-tk',
'/opt/virtenvs/django_slice/lib/python2.7/lib-old',
'/opt/virtenvs/django_slice/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk',
'/opt/virtenvs/django_slice/local/lib/python2.7/site-packages']
Server time: Fri, 11 Oct 2013 22:06:43 +0000
And it occurs in /django/contrib/admin/templates/admin/change_form.html
32 <li>{% trans "History" %}</li>
Here is the (possible) relevant urls:
/admin/core/app/ HANDLER: changelist_view
/admin/core/app/add/ HANDLER: add_view
/admin/core/app/(.+)/history/ HANDLER: history_view
/admin/core/app/(.+)/delete/ HANDLER: delete_view
/admin/core/app/(.+)/ HANDLER: change_view
My guess would be you have a view referenced somewhere in your urlconf which can't be imported, which would cause reverse to fail. Try reversing a different view from the shell.
This could be caused by having an entry with an empty primary key, which could happen if your model has a field like
id = models.CharField(blank=True, primary_key=True)
I am going to comment on this as I had EXACTLY the same issue and this was the only link I could find that pointed me in the right direction. Actually it didn't point me in the right direction but at least it made me feel less alone :)
Note my code did work sometimes. In fact my wrong code is still working happily on a production server.
My problem was in admin I had overridden my render_change_form. When I called the super method instead of doing
return super(ReceiptAdmin, self).render_change_form(request, context, *args, **kwargs)
I had inadvertently done
return super(ReceiptAdmin, self).render_change_form(request, context, args, kwargs)
I find this out when I stepped through the code and noticed what my parent method was being passed didn't look right. It mystifies me why it works sometimes. This incorrect call was copied and pasted from some other code which is also failing locally but not on live.
Anyway, there if any use for anybody else.

raise 404 instead of 500 in urls.py django

I have a website powered by django. There are few url patterns which i donot catch
say www.mysite.com/12233445 www.mysite.com/###$$$ www.mysite.com/alphabetagamma this leads to 500 error page, i want all such non-standard urls to redirect to 404 error page. Any clue which is the best method of doing it?
By using custom handler500ΒΆ you can change the response status code from 500 to 404. make your project to return this post may help you get the idea how to do it - http://ilian.i-n-i.org/custom-404-not-found-page-with-django-cms/
But as #Daniel said it isn't normal to get 500 for not found pages. Turn on the debug mode and check what error leads to 500.
Have you created a 404.html template? This happens when you have DEBUG = False in your settings.py, yet you haven't created a 404.html template. This raises a TemplateDoesNotExist exception, hence the error 500 response.
go to your views, perform all the get/filter operations within a try except block.
try:
variable = object.get(some checks here)
except: object.DoesNotExist
raise Http404
return (whatever)

Django error: 'QueryDict' object has no attribute '_meta'

I am very new to django so this is probably a noob question.
I am trying to reuse django admin's change list view. I've created a admin model and want to provide the changelist template a list of these objects. In my view I have:
def placements(request):
partner_id = request.session.get('partner_id', 0)
self = PlacementAdmin(request.GET, Placement.objects.filter(partner=partner_id))
return render_to_response('publisher/placement/change_list.html', {'cl': self})
I get this error when I try to hit this function from a browser:
'QueryDict' object has no attribute '_meta'
Could anyone tell me what the error is or provide an easier way to accomplish this in case I am on the wrong track completely.
Heres the complete trace:
Request Method: GET
Request URL: http://localhost:8080/publisher/
Django Version: 1.3 beta 1
Exception Type: AttributeError
Exception Value:
'QueryDict' object has no attribute '_meta'
Exception Location: /Users/imran/django_env/lib/python2.6/site-packages/django/contrib/admin/options.py in __init__, line 278
Python Executable: /Users/imran/django_env/bin/python
Python Version: 2.6.1
Python Path:
['.',
'.',
'/Users/imran/Workspaces/publisher/django/pub_admin',
'/Users/imran/django_env/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg',
'/Users/imran/django_env/lib/python2.6/site-packages/pip-0.8.1-py2.6.egg',
'/Users/imran/django_env/lib/python26.zip',
'/Users/imran/django_env/lib/python2.6',
'/Users/imran/django_env/lib/python2.6/plat-darwin',
'/Users/imran/django_env/lib/python2.6/plat-mac',
'/Users/imran/django_env/lib/python2.6/plat-mac/lib-scriptpackages',
'/Users/imran/django_env/Extras/lib/python',
'/Users/imran/django_env/lib/python2.6/lib-tk',
'/Users/imran/django_env/lib/python2.6/lib-old',
'/Users/imran/django_env/lib/python2.6/lib-dynload',
'/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6',
'/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages',
'/Users/imran/django_env/lib/python2.6/site-packages']
It's best to post the traceback error when posting a question!
That said, the error comes from you passing request.GET to a ModelAdmin admin object which isn't expecting it.
You have a long road of troubles ahead of you though, as you're trying to use the magical change_list view template which is operated on by a collection of even more magical, undocumented template tags, which just doesn't normally appear in the same sentence as new to django.
I think you're on the wrong track simply because django's admin is not easy to hack into.
At a minimum, you'd have to pass in your template a ChangeList object for the cl variable.
If you really want to do this, the only advice I can give is to take a look at django.contrib.admin.options.ModelAdmin.changelist_view() since that's what you're trying to replicate.
Seriously though I'd like to talk you out of this. Django's actually really fun to work with!

django when edit the data from data base

i am tryong to edit the data from database i am getting error at update_user_id for more info please look at this link then u will understood please
http://www.pastie.org/1301839
It seems that you didn't set the update_user_id key in your session. You can work around this problem using exceptions:
try:
update_user_id = request.session["update_user_id"]
except KeyError:
update_user_id = SOME_DEFAULT_VALUE
Or (even better) by using request.session.get:
update_user_id = request.session.get("update_user_id", SOME_DEFAULT_VALUE)
The two snippets are equivalent.
From your traceback:
Exception Type: KeyError at /institutes_admin/
Exception Value: 'update_user_id'
A KeyError means that you tried to get something from a dictionary using a dictionary key that didn't exist. This looks like the problem (from further up the traceback):
File "/home/karthik/Desktop/EPW_LOCAL/mysite/../mysite/epw/admin_views.py" in institutional_ip_admin
604. update_user_id = request.session['update_user_id']
A solution would be to provide a default value.