Auto populate django models and views while creating an app - django

I am pretty new to Django and still learning. The question is that i know that an empty app can be created in django and then i can add models and views. But throughout a long time of making websites, I noticed that most of the time i am doing operating on dbs like update,del or insert. Now my question is that is there anyway that i can add the model parameters while creating the app itself in django and it automatically populated models,forms and views for me for the basic operations that i mentioned above.
That way i can create DBS architecture while i am creating the app and the only way that i am left with is to review and make changes in models if any.
Just to make sure that i am understood, i am attaching a snippet of what i am trying to say which is wrong but i hope that it gives a better idea
//Not real code Purpose only to give a better understanding to question
python manage.py startapp test --fields id=int verified=bool
Or if there is not is there any zsh plugin / 3rd party plugin that might be helpfull
Also i should mention that the need originated from a current project which is a one page application and has lots of tables but no operations on it. So the task to write models everytime would be tedious and not the correct way. So any other suggestions are also greatly appreciated

Related

Integrating Django tutorial example Polls app and django-registration

I'm learning Django on Ubuntu 13.04, Python 2.7, Django 1.5, Postgres 9.2,
Bootstrap 3.0. I'd like to achieve a combination of the tutorial example Polls app with django-authentication.
As my first effort I got the Polls app working from the Django 1.5 tutorial. I then installed django-registration 1.0 and these templates to make it work. I chose that package for authentication as opposed to django-allauth as a result of my question on authentication framework.
Now I want to integrate Polls and django-registration to record a set of results per user. After the poll results have been collected the admininstrator uses Django Admin interface to run a script to analyse the results (e.g. compute some statistics) and send an email to a subset of all users.
I briefly looked at two existing projects that looked like could get me there out of the box.
Light Bird's Questionnaire App was too complicated using a custom library of modular class based views. I'd like to keep it as simple as possible, using as much of out-of-the-box Django 1.5 functionality as possible for ease of maintenance and initial design.
Pinax web framework on top of Django, although a great idea, seems to be stuck in dark ages of 2011 with latest code supporting only Django 1.4 and Bootstrap 2.x. Starter projects don't look that useful and documentation isn't flash either.
Based on the above it looks like I'll have to do the integration of Polls and registration manually. At first pass I was thinking roughly the following:
The poll & choice could be simplified down to just a numeric answer to a question.
At database level we would need a separate table.
The primary key would be the userid.
Each column would store one answer per.
I'm guessing this would need a class PollsResults in model.py that would include defining the primary key as User, which should exist via django-registration.
Exactly how to do that and what follows gets a bit hazy to me at the moment.
I'm sure the above is a simple exercise for a Django developer. Could anyone give me some starting hints or even better an existing project that does something similar?
It looks like you're slightly underestimating the power of using a framework such as django. For example, you don't really need to worry too much about tables in the database or what will be their primary keys, because django's Object Relational Mapper (ORM) takes care of a lot of that for you.
If you want to connect two models (database tables) in django you can use a foreignkey like this:
class ThingOne(models.Model):
name = models.CharField(max_length=50)
class ThingTwo(models.Model):
thing_one = models.ForeignKey('ThingOne')
The quotes around 'ThingOne' in my ForeignKey are actually unnecessary because the ThingOne model has already been defined, but I like to use quotes anyway because it means your ForeignKeys will also work for models defined below (in your code) the model linking to them.
You therefore just need to add a relationship between your Polls and User models. If one user might have many poll results you should probably use a ManyToManyField instead of a ForeignKey but the principle is the same. That should be enough to get you started.

Turning a usual django website into a RESTful api

I've been working on. A web project for few months now.And it's a simple website with usual django models views urls and templates,with simple Ajax using jquery for the templates.
Now thinking of having a possibility of making an application soon. I am thinking of using Tastypie to make a RESTful api for my website. So the idea is to
Use Tastypie to make a RESTful api.
Using backbone.js to make my website a single page application
Using plist and json for my android and ios applications.
My concern starts here, with this new course of action, would. I have to make most of my website again? And if that is the case should I stop working on what I've already made and then start from scratch following the above guidelines? Again, in context to tastypie and backbone.js, what would happen to my already written views and template logic?
I've been reasearching over this topic alot and cannot find a definitive answer. So thought of asking all of you who are already experienced or have gone through the same transition.
I have almost no exposure to RESTful APIs. But I am getting inclined towards a RESTful approach as it would make app development easier.
PS. I would really appreciate if I don't get negative votes for it, but advice instead.
You could try it out side by side in the same site/project. Keep the existing django views. Add new tastypie ones under a different URL, for instance "/api/".
Likewise, a single page app? That is just one new template next to the existing ones. Keep it at "/single/" for the moment. And keep the existing pages.
The actual functionality will probably stay the same. Extract useful code as separate functions out of your existing views and call them from both tastypie and your original views. This will probably even make your existing code clearer and cleaner :-)
I'd say it's much of a philosophical question.
If the site is on a deadline or in production I would probably put it in git and give it two head branches:
production branch
refactor branch
The benefit then is that you can keep the old site up and add new features and merge this in your refactor branch.
Then again it much depends on the complexity and how the site acts today.
Doing the two branch system will give you the option to stop the refactor without losing your old version.

Design Issue: To what extent should the Django Admin site be used?

I'm relatively new to Django and especially new to the admin functionality. I've read many articles expounding the benefits of the built-in admin site but I'm not sure to what extent they should be used.
Some general questions:
What sort of database interactions should be in admin and which should be part of the general site functionality?
I understand that models can be easily changed/deleted on the admin site but is it the best way to add new objects rather than in a custom page?
A more specific question:
I'm creating a website for tracking packages. The idea is that staff would scan and insert many (hundreds of) packages into the database on a daily basis. I'm unsure whether this could be done efficiently in admin as the current process of adding a model is a slow, multi-step process (I don't know to what extent that can be customized). As this is one of the site's main functionalities it also seems odd that it would reside in the admin site. Would it make more sense in the Django framework to write my own page? Or would I just be rewriting (probably less efficiently) what's already been created with django-admin?
Any suggestions or Django standards you guys have would be greatly appreciated.
Generally the django-admin should be used for CRUD (Create, Read, Update and Delete) operations. It should be used where you want to quickly setup a facility to allow people to manage the data. It can handle complex database relationships and inline editing, however it would be more appropriate to build a seperate django application if your adminstration facility is going to be bespoke. Django-admin can be customised heavily and you can subclass its inner workings, however it's a lot easier in the long run to code and maintain your own application albeit it will take longer to get it up and running.
One way of finding out if the django-admin is appropriate to use or not is to pick one important requirement and try to implement it, if it feels like it's a lot of work trying to implement it in django-admin then it's probably a case of shoehorning. I think sometimes requirements are shoehorned into django-admin by developers who think it's a one-size-fits-all solution which it simply isn't.

How to create views automatically from model as in Django admin?

I'm starting to use Django and I'm really impressed by the possibility of automatically creating views from a model.
So, I'd like to know if there is such functionality on other web frameworks. I know of RoR scaffolding, but that is not quite the same thing, since you need to change your views manually in case you change the related model.
For those who are looking for an answer here, perhaps django-baker (https://github.com/krisfields/django-baker.git) will do what you need.
pip install django-baker
Then add django_baker to INSTALLED_APPS and run
python manage.py bake your_app_name
Note that previously you need to remove urls.py and views.py from your app so they can be generated.
Then it will generate all the skeleton stuff.
https://docs.djangoproject.com/en/dev/topics/class-based-views/
I think I've found one possible solution. The concept is called model drive development. There are quite a few java frameworks that provide the ability to create a, rather simple, application directly from the model. This post presents some of them http://www.javaneverdie.com/java-frameworks/java-domain-driven-frameworks-review/

How to port from Drupal to Django?

What would be the best way to port an existing Drupal site to a Django application?
I have around 500 pages (mostly books module) and around 50 blog posts. I'm not using any 3rd party modules.
I would like to keep the current URLS (for SEO purposes) and migrate database to Django. I will create a simple blog application, so migrating blog posts should be ok. What would be the best way to serve 500+ pages with Django? I would like to use Admin to edit/add new pages.
All Django development is similar, and yours will fit the pattern.
Define the Django model for your books and blog posts.
Unit test that model using Django's built-in testing capabilities.
Write some small utilities to load your legacy data into Django. At this point, you'll realize that your Django model isn't perfect. Good. Fix it. Fix the tests. Redo the loads.
Configure the default admin interface to your model. At this point, you'll spend time tweaking the admin interface. You'll realize your data model is wrong. Which is a good thing. Fix your model. Fix your tests. Fix your loads.
Now that your data is correct, you can create templates from your legacy pages.
Create URL mappings and view functions to populate the templates from the data model.
Take the time to get the data model right. It really matters, because everything else is very simple if your data model is solid.
It may be possible to write Django models which work with the legacy database (I've done this in the past; see docs on manage.py inspectdb).
However, I'd follow advice above and design a clean database using Django conventions, and then migrate the data over. I usually write migration scripts which write to the new database through Django and read the old one using the raw Python DB APIs (while it is possible to tie Django to multiple databases simultaneously, too).
I also suggest taking a look at the available blogging apps for Django. If the one included in Pinax suits your need, go ahead and use Pinax as a starting point.
S.Lott answer is still valid after years, I try to complete the analysis with the tools and format to do the job.
There are many Drupal export tools out of there by now but with the very same request I go for Views Datasource choosing JSON as format. This module is very solid and available for the last version of Drupal. The JSON format is very fast in both parsing and encoding and it's easy to read and very Python-friendly (import json).
Using Views Datasource you can create a node view sorted by node id (nid), show a limited number of elements per page, configure a view path, add to it a filter identifier and pass to it the nid to read all elements until you get an empty JSON response.
When importing in Django you have a wide set of tools as well, starting from loaddata to load fixtures. Views Datasource exported JSON but it's not formatted as Django expects fixtures: you can write a custom admin command to do the import, where you can have the full control of the import flow.
You can start your command passing a nid=0 as argument and then let the procedure read, import and then fetch data from the next page passing simply the last nid read in the previous HTTP request. You can even restrict access to the path on view but you need additional configuration on the import side.
Regarding performance, just for example I parsed and imported 15.000+ nodes in less than 10 minutes via a Django 1.8 custom admin command on an 8 core / 8 GB Linux virtual machine and PostgreSQL as DBMS, logging success and error information into a custom model for each node.
These are the basics for import/export between these two platform, for detailed information I described all the major steps for export from Drupal and then import to Django in this guide.