oscarmlage / django-cruds-adminlte

django-cruds is simple drop-in django app that creates CRUD for faster prototyping
BSD 3-Clause "New" or "Revised" License
425 stars 82 forks source link

Ajax inline #12

Closed luisza closed 7 years ago

luisza commented 7 years ago

This PR include:

New Inlines for update views based in djangoajax system Fixed UserCRUDView Include pip package fixes Fixed Detailview presentation Include list_fields and display_fields for CRUDVIEW as django admin Support different types of pk because regex is now [\]+ and not \d+

If it's accepted I suggest include djangoajax as dependency This is an example how it's works

from cruds_adminlte.crud import UserCRUDView
from cruds_adminlte.inline_crud import InlineAjaxCRUD

from .models import Institution, Notification_URL
class Notification_URLAjaxCRUD(InlineAjaxCRUD):
    model = Notification_URL
    base_model = Institution
    inline_field = 'institution'
    fields = ['description', 'url']
    title = "Direcciones de notificación"

class InstitutionCRUD(UserCRUDView):
    model = Institution
    check_login = True
    check_perms = True
    fields = ['name', 'domain', 'actived']
    list_fields = ['name', 'domain', 'actived']
    display_fields = [
        'name', 'code',  'domain', 'actived', 'private_key', 'server_public_key', 'public_certificate']

    inlines = [Notification_URLAjaxCRUD]

The result is something like

pantallazo

Maybe some tests are needed

oscarmlage commented 7 years ago

Just one question, the above example code needs to be placed in views.py, right?. Do you think is there any way to put it in a separate file just to not merge cruds stuff with other stuff in app (front views, api, etc...) as admin does with admin.py? Do you think it worths the effort to have it isolated in any way?.

luisza commented 7 years ago

All InlineAjaxCRUD code are in inline_crud.py so it's separated of other stuff, the example code above not work because you don't have this models (I am working in other project using django-cruds-adminlte so I tested with it) but it's good example how it's work. Maybe I will update the demo project but I don't know when. As I see the user needs to create this classes in his views.py, and change per model so the separation there is the user using the generic classes provided by django-cruds-adminlte.

oscarmlage commented 7 years ago

Following instructions, I've:

luisza commented 7 years ago

Did you include urls ?

In urls.py

import AutorCRUD  # correct import path

authorcrud= AutorCRUD()
urlpatterns = [ 
         ...,
       url(r'path/', include(authorcrud.get_urls()))
]
oscarmlage commented 7 years ago

After include urls.py it needed a column/foreignkey.html but now it works properly:

image

OTOH, loading the authors list (http://127.0.0.1:8000/testapp/autor/list) it gives an error related to crud.py::UserCRUDView::get_list_view::UListView::get_queryset() in line 482 (the Autor model has no user field:

queryset = queryset.filter(user=self.request.user)

image

luisza commented 7 years ago

Yes, because you are using UserCRUDView. If not user field then use CRUDView. Some documentation is missing, sorry.

oscarmlage commented 7 years ago

Nice, it runs, thank you so much!. I've changed a bit the behavior and the inline layout with bootstrap modals: e4e3f0b0b924544f2cb90305fc3c17d5d8dccb0b

oscarmlage commented 7 years ago

I've found an issue (as your repo has no issues, dunno if this is the right place to comment it).

I've tried to use a custom form with inlines and it failed (the form shown is not the custom one, plus other problem with the list).

I have two models: Invoice and Line. Without inlines the I've customized the Invoice form with fields and tabs and it looks like something like this:

image

Once I active the inlines code:

views.py

class Lines_AjaxCRUD(InlineAjaxCRUD):
    model = Line
    base_model = Invoice
    inline_field = 'invoice'
    fields = ['reference', 'concept', 'quantity', 'unit', 'unit_price',
              'amount']
    title = _("Lines")

class InvoiceCRUD(CRUDView):
    model = Invoice
    check_login = True
    check_perms = True
    fields = '__all__'
    list_fields = '__all__'
    display_fields = '__all__'
    inlines = [Lines_AjaxCRUD]

urls.py

invoicecrud = InvoiceCRUD()
urlpatterns = [
    ....
    url(r'', include(invoicecrud.get_urls()))
]

custom_forms = {
    'add_invoice': InvoiceForm,
    'update_invoice': InvoiceForm
}
urlpatterns += crud_for_app('testapp', login_required=True,
                            check_perms=True, modelforms=custom_forms)

In the same url (http://127.0.0.1:8000/testapp/invoice/1/update) I got something like this:

image

Is there any other way to include the InvoiceCRUD with a custom_form?.

OTOH, trying to load list url (http://127.0.0.1:8000/testapp/invoice/list) I got something like this:

image

If I delete the include url in urls.py (url(r'', include(invoicecrud.get_urls()))) it works, but without the inlines part.

oscarmlage commented 7 years ago

Ok, the second issue (the one related to list, is because fields = '__all__' is not allowed, if I describe all the fields it works.

views.py

class InvoiceCRUD(CRUDView):
    model = Invoice
    check_login = True
    check_perms = True
    fields = ['customer', 'registered', 'sent', 'paid', 'date', 'invoice_number', 'description1', 'description2', 'subtotal', 'subtotal_iva', 'subtotal_retentions', 'total']
    list_fields = ['customer', 'registered', 'sent', 'paid', 'date', 'invoice_number', 'description1', 'description2', 'subtotal', 'subtotal_iva', 'subtotal_retentions', 'total']
    display_fields = ['customer', 'registered', 'sent', 'paid', 'date', 'invoice_number', 'description1', 'description2', 'subtotal', 'subtotal_iva', 'subtotal_retentions', 'total']
    inlines = [Lines_AjaxCRUD]
oscarmlage commented 7 years ago

And the first issue is solved too, adding the add_form and update_form attributes to the CRUDView it worked:

views.py

class InvoiceCRUD(CRUDView):
    model = Invoice
    check_login = True
    check_perms = True
    add_form = InvoiceForm
    update_form = InvoiceForm
    fields = ...

image

Sorry for the noise!