Re: Django 1.9 - Create an app that use the models from another app and dynamic CBV

74 views
Skip to first unread message
Message has been deleted

James Schneider

unread,
Jun 2, 2016, 4:18:45 AM6/2/16
to django...@googlegroups.com


On Wed, Jun 1, 2016 at 5:21 AM, <sevenr...@gmail.com> wrote:

I'm creating a basic CMS. The CMS will contain information about different Entities.


A normal users can only see the information about the Entities but an editor will have access to an Dashboard were it can add new Entities or updated them.

I have an app Entity that use class-based view (CBV) for create,update,details,list-display (CRUD).

You'll probably need to implement a permission system to control access to resources.  


The urls for listing and details are: /entities and /entity-name


Because update and create are behind login and use different urls I create a second app Dashboard that use the create/update models of the Entity app

One app can handle multiple URL schemes, both for anonymous and authenticated requests simultaneously. You really only need multiple apps when there is a clear separation of duties/functionality. They're really more for organization of the code than any functional/technical reason. 


The urls are account/add and account/edit.


There is another solution to have different urls for views of the same model ?



You can have as many views as you'd like for the same model. It is common in situations where an object can be in different states at different times. For example, an account is created for a user, so the user needs to claim it, and then the new user needs to modify their own account with the right details and change passwords, etc. That may also include having multiple forms for the same object as well. 

I want the fist page of the dashboard to show different information based on the following logic(exist and/or is activated):

  • if doesn't exist I want the CreateView (CBV) and corresponding form to appear
  • if exist but it is not active a Pending message
  • if exists and active the data of the entity

I can do the logic at the template level, but to separate things is better to be done at the CBV level. So in this case I need some help, how to implement it.


Well, yes, and no. I wouldn't rely solely on the template to handle this behavior. You'll be twisting yourself into a pretzel to make it work. There's a couple of ways to make this work:
  • Have a single view that initially examines the situation, and then calls the respective functions or CBV's that handle the specific situation. Keeps the request logic within the view infrastructure, but not a typical strategy as far as I know. The same URL can then serve all of the situations.
  • Have a single view that builds a basic page structure, and use a JS framework and AJAX calls to build an SPA that displays the right information.
  • Have a separate view for each situation, along with a separate URL, and another view/URL for the main dashboard. Have the main dashboard view check for your logic, and redirect the browser to the URL for each action. Once the situation is corrected (form submitted, etc.) then redirect back to the main dashboard. This would be my preferred method.
You can use template inheritance in all of these situations to keep the amount of template code down (which is generally advised since the template system tends to be the slowest part of Django). 

-James

sevenr...@gmail.com

unread,
Jun 2, 2016, 5:51:12 AM6/2/16
to Django users
Thanks you James,

To make it more clear.  Let's say I have companies(entities) and products(sub-entities) to be more clear. Regarding  the urls I use the main urls.py to include the urls.py from each app.

urlpatterns = [
    url
(r'^admin/', admin.site.urls),
    url
(r'^companies/', include('company.urls', namespace='company'))

 In the entity app the urls.py :

urlpatterns = [
    url
(r'^$', CompanyListView.as_view(), name='list'),
    url
(r'^add/$', EntityCreateView.as_view(), name='create'),



Normal View

/companies/  - listing all
/companies/company-name/ - details about a company
/companies/products/ - listing all products of a company
/products/ - listing all products
/products/product/  - details about a product

Editor

/account/       - dashboard)
/account/add   - adding a company, the company is actually the account, no other info for account, and is onetoone relationship with user
/account/edit -update company data
/account/company-name/products/add - add a product to company
/account/company-name/product-name/edit - update product info

The account app I try to use is to control the account, even if doesn't exist a model. I understand that I can use multiple urls patterns in urls.py for an app in general, but can you gave me an example for the case above ? (having just two apps companies and products)

-------- At the view level ----------------------------

In the Dashboard(account) first I verify if the company exist and activated(approved by admin(boolean field).

If the company doesn't exist(first time) show the CreateView(CBV) form, if exists and activated show the company info and others and if it is not activated show a Pending message.

Based on the solutions you presented, I think is having a generic view (empty with only logic or with one of the cases(pending,exist and active)) and to redirect to different urls and corresponding views. 

Or you think there is a better approach ?













Reply all
Reply to author
Forward
0 new messages