Closed luisza closed 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?.
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.
Following instructions, I've:
added models in models.py
in demo project like the following:
class Autor(models.Model):
name = models.CharField(max_length=200)
class Addresses(models.Model):
autor = models.ForeignKey(Autor, related_name="Autor")
address = models.CharField(max_length=100)
city = models.CharField(max_length=100)
created views.py
like the following:
from cruds_adminlte.crud import UserCRUDView
from cruds_adminlte.inline_crud import InlineAjaxCRUD
from .models import Autor, Addresses
class Address_AjaxCRUD(InlineAjaxCRUD):
model = Addresses
base_model = Autor
inline_field = 'autor'
fields = ['address', 'city']
title = _("Addresses")
class AutorCRUD(UserCRUDView):
model = Autor
check_login = True
check_perms = True
fields = ['name']
list_fields = ['name']
display_fields = ['name']
inlines = [Address_AjaxCRUD]
But in the end I couldn't manage to see the inlines in the "http://127.0.0.1:8000/testapp/autor/1/update" url. Dunno if I missed some step.
Did you include urls ?
In urls.py
import AutorCRUD # correct import path
authorcrud= AutorCRUD()
urlpatterns = [
...,
url(r'path/', include(authorcrud.get_urls()))
]
After include urls.py
it needed a column/foreignkey.html
but now it works properly:
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)
Yes, because you are using UserCRUDView. If not user field then use CRUDView. Some documentation is missing, sorry.
Nice, it runs, thank you so much!. I've changed a bit the behavior and the inline layout with bootstrap modals: e4e3f0b0b924544f2cb90305fc3c17d5d8dccb0b
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:
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:
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:
If I delete the include url in urls.py (url(r'', include(invoicecrud.get_urls()))
) it works, but without the inlines part.
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]
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 = ...
Sorry for the noise!
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
The result is something like
Maybe some tests are needed