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

CRUD #21

Closed
thegeorgeous opened this issue Feb 19, 2014 · 27 comments
Closed

CRUD #21

thegeorgeous opened this issue Feb 19, 2014 · 27 comments

Comments

@thegeorgeous
Copy link

Simple CRUD query to get all objects returns HTML page instead of object, generating the following error

Error: [$resource:badcfg] Error in resource configuration. Expected response to contain an array but got an object
@jkosir
Copy link
Collaborator

jkosir commented Feb 19, 2014

Can you add some code? Can't see what's wrong without it.

@thegeorgeous
Copy link
Author

This is the js file I used. I'll put gists of the other files if necessary
https://gist.github.com/thegeorgeous/9086515

@jkosir
Copy link
Collaborator

jkosir commented Feb 19, 2014

The problem is that you redefine the query method with isArray = false, while query returns an array of objects.

@thegeorgeous
Copy link
Author

Well removing that gives me the same error. I seem to be getting an html page instead of the the model object

@jkosir
Copy link
Collaborator

jkosir commented Feb 19, 2014

The html is probably django debug page, so the issue seems to be in the django code. Angular is set up correctly after fixing the isArray quirk.

What does the html page say?

@thegeorgeous
Copy link
Author

This is the html
https://gist.github.com/thegeorgeous/9089386

@jkosir
Copy link
Collaborator

jkosir commented Feb 19, 2014

It looks like the view you have on crud/task isn't a subclass of CRUD view, since you get a html response rather than json data.

@thegeorgeous
Copy link
Author

So how do I make it a subclass?

@jkosir
Copy link
Collaborator

jkosir commented Feb 19, 2014

There's an example in the docs. You need 2 views, one that returns html page and another one that is a subclass of NgCRUDView.

@thegeorgeous
Copy link
Author

You mean like this

class TaskCRUDView(NgCRUDView):
    model_class = Task

I already have that in views.py

@jkosir
Copy link
Collaborator

jkosir commented Feb 19, 2014

Yes, exactly. I guess crud/task doesn't point to this view in urlconf then.

@thegeorgeous
Copy link
Author

Actually it does

...
url(r'crud/task$', TaskCRUDView.as_view(), name='task_view'),
...

@jkosir
Copy link
Collaborator

jkosir commented Feb 19, 2014

Well if you get a HTML response there must be something wrong in the urlconf. NgCRUDView never returns html. Probably you have a entry in urlconf that matches crud/task and points to the HTML view.

@thegeorgeous
Copy link
Author

No, there are no other entries for crud/task.
This is my views.py

class TaskCRUDView(NgCRUDView):
    model_class = Task

def viewTask(request):
    return render(request, 'viewTask.html')

and these are the url patterns I have

    url(r'^admin/', include(admin.site.urls)),
    url(r'^timesheet/viewtask/', 'timesheet.views.viewTask', name='viewTask')
    url(r'crud/task$', TaskCRUDView.as_view(), name='task_view'),

@jkosir
Copy link
Collaborator

jkosir commented Feb 20, 2014

So just to be clear, Task.query() returns a HTML page, the one viewTask should be returning?

Can you add some django logs?

@thegeorgeous
Copy link
Author

Yes exactly. I'm getting a runtime error

RuntimeError: maximum recursion depth exceeded

@jkosir
Copy link
Collaborator

jkosir commented Feb 20, 2014

Could you post the whole traceback at least?

Or alternatively the whole code if you're okay with that.

@thegeorgeous
Copy link
Author

@dasf, here is the whole code
https://github.com/thegeorgeous/tripping-octo-tyrion

@jkosir
Copy link
Collaborator

jkosir commented Feb 21, 2014

Oh, the problem is that ForeignKey fields can't be serialized, which raises an Exception. I will make a fix and create a pull request.

As a temporary workaround you can remove the fk field in your view.

class TaskCRUDView(NgCRUDView):
    model_class = Task

    def get_model_fields(self):
        fields = super(PostView, self).get_model_fields()
        fields.remove('client_name')
        return fields

@jkosir
Copy link
Collaborator

jkosir commented Mar 1, 2014

This has now been fixed in 0.6.1

I suggest you reinstall django-angular to get the latest version. If you still have problems please reopen the issue.

@jkosir jkosir closed this as completed Mar 1, 2014
@thegeorgeous
Copy link
Author

@dasf, Sorry for following this up a little late, but I tried using a model without foreign key and that didn't work either. I'm still getting html instead of json.

@jkosir
Copy link
Collaborator

jkosir commented Mar 4, 2014

Did you update django-angular? Also if you still get HTML response please post the response you're getting.

@thegeorgeous
Copy link
Author

@dasf, yes I did update django-angular to version 0.6.1 The HTML response is view returned by viewTask in the code
https://github.com/thegeorgeous/tripping-octo-tyrion

@jkosir
Copy link
Collaborator

jkosir commented Mar 5, 2014

After a while of trying I can't get your code to run, getting some weird errors, might look into it when I have more time.

If you're using the latest version from https://github.com/thegeorgeous/tripping-octo-tyrion, the problem is you have the temporary fix for 0.6.0 in your code. And that won't work with 0.6.0 either since you're calling the wrong view in super().

This works now, without overriding the get_fields() method.

class TaskCRUDView(NgCRUDView):
    model_class = Task

If after this you're still getting any errors please attach some django logs.

@thegeorgeous
Copy link
Author

This is what I get in the Firebug console

Error: [$resource:badcfg] Error in resource configuration. Expected response to contain an array but got an object
http://errors.angularjs.org/1.2.13/$resource/badcfg?p0=array&p1=object
minErr/<@https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js:78
resourceFactory/</Resource[name]/promise<@http://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular-resource.js:526
qFactory/defer/deferred.promise.then/wrappedCallback@https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js:11033
qFactory/defer/deferred.promise.then/wrappedCallback@https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js:11033
qFactory/ref/<.then/<@https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js:11119
$RootScopeProvider/this.$get</Scope.prototype.$eval@https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js:12045
$RootScopeProvider/this.$get</Scope.prototype.$digest@https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js:11871
$RootScopeProvider/this.$get</Scope.prototype.$apply@https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js:12151
done@https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js:7909
completeRequest@https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js:8110
createHttpBackend/</xhr.onreadystatechange@https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js:8056

https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js
Line 9503

and this is the djangologs I get

[06/Mar/2014 10:15:10] "GET /timesheet/viewtask/ HTTP/1.1" 200 1251
[06/Mar/2014 10:15:11] "GET /timesheet/viewtask/crud/task? HTTP/1.1" 200 1251

@jkosir
Copy link
Collaborator

jkosir commented Mar 6, 2014

[06/Mar/2014 10:15:10] "GET /timesheet/viewtask/ HTTP/1.1" 200 1251
[06/Mar/2014 10:15:11] "GET /timesheet/viewtask/crud/task? HTTP/1.1" 200 1251

The problem is that you have 'crud/task' in angular service config, so when making a request it appends that to current path, thus making a request to '/timesheet/viewtask/crud/task'. Which is matched by regex for your normal HTML view. In your case you need '/crud/task' in the service config, so that when making a request from /timesheet/view the query goes to 'crud/task', without the timesheet part.

@thegeorgeous
Copy link
Author

That worked!!! Thanks for bearing with me @dasf !! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants