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

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!

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?

TypeError: expected string or bytes-like object User.id

I'm trying to register an new Transaction object on the DB using Django, but I'm having TypeError: expected string or bytes-like object when I try to do user_id = user.id I can't really understand why this is happening, since I do the same steps when registering a new Bank object (as shown on prints below). I've tried to debug and the local variables have the correct value, I've also tried to cast user.id with string or int, but none of them worked.
traceback console error create Transaction method create Bank method
models.py
Firstly, please don't post code or errors as images; they are text, they should be posted as text in the question.
However I don't see anything in any of those snippets that suggest the error is with the user - that line is probably highlighted because it's the last in that multi-line call.
Rather, the error looks to be in the reference to date.today - if that's the datetime.date class, then today is a method, which you would need to call:
Transaction.objects.create(date=date.today(), ... )
Or, since that field has a default anyway, you could leave out the date attribute from the create call altogether.

Getting "AttributeError: 'str' object has no attribute 'regex'" in Django urls.reverse

As in the title, I'm getting
AttributeError: 'str' object has no attribute 'regex'
I've seen many valid answers to this, but none of them applied to my code as they identified issues in the urls.py file. My issue is in the views.py file.
I get the exception when using reverse to generate a redirect URL:
HttpResponseRedirect(reverse('changetracker:detail', args))
When I use 'changetracker:detail' in other functions, I don't get the exception.
I'm answering this to share knowledge as I didn't see this particular root cause identified already.
The problem turned out to be that I should be using a keyword argument 'args=args' to pass in URL arguments, not a positional argument:
HttpResponseRedirect(reverse('changetracker:detail', args=args))
Using the positional argument (position 2) caused reverse to use that argument as the URL list and thus raise the AttributeError.

is Django AdminForm missing has_changed?

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.

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.