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

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/

Related

Django - Where to create classes to manipulate database

Where i must create classes in order to manipulate data of the database?
I have 4 app in my project. Can i create an app specially for that, and create all the classes/functions in the models.py ?
The better approach is to have each app its own models.py. which would define the database tables each app is using.
And to have each app its own views.py. which would define the logic of the app and manipulation of the data of your database.
I don't think you really understand what apps really are in Django and you can learn about them in here look at other answer and mine too.
yes, it's recommended to create your models in each of your apps directory for reusability.
edit* check out this answer if you're wondering how to implement singleton in django,

Auto populate django models and views while creating an app

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

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.

Updating django pluggable apps after modification

I'm using django-basic-apps' blog app, but I have a few modification to it: added a ManyToManyField and a URLField.
Is it possible to maintain my modifications while keeping up-to-date with the most recent release of the apps?
Sure, just fork the project on GitHub. You can then regularly merge from the parent project, while keeping your modifications.
You can do this via your own git repo as well, if you don't want to make your modifications public, but GitHub is easier as it has all this set up for you already.
It might be best to extend the existing models from django-basics-apps, have a look at:
http://docs.djangoproject.com/en/1.2/topics/db/models/#model-inheritance
# custom_blog/models.py
from blog.models import Post
class CustomPost(Post):
new_field_one = ManyToManyField
new_field_two = URLField

Pinax: Customize Signup and profile

I want to gather some more information when the user signs up and also display this as part of the profile information - so that the user can edit it once he creates a login.
How can I extend the sign-up and profile form and model without changing directly pinax code?
From pinax docs
Customization
As more sites are built using Pinax,
more best practices will emerge, but
for now what we recommend is:
Always work off a stable release. The most current release is 0.7.1.
Use the pinax-admin setup_project command.
Make necessary changes to the settings.py and urls.py files in your copied directory.
Change the domain and display name of the Site in the admin interface.
Develop your custom apps under your new project or anywhere on Python path.
Develop your own templates under your new project.
This thread is very relevant to your question and discusses options for overriding the default pinax apps.
It suggests looking at https://github.com/eldarion/idios, (an extensible profile app designed to replace the profiles apps in Pinax).
This blog has some comments about overriding the default pinax apps:
Overriding build-in Pinax Applications
Assuming we want to override
$PINAX_ROOT/apps/blog with
$PROJECT_ROOT/apps/blog we can do so
simply by copying the application and
make our project-local (read
individual) changes to it. Pinax will
then load $PROJECT_ROOT/apps/blog
rather than $PINAX_ROOT/apps/blog.
Those who do not care about merging in
upstream changes nor submitting
bugfixes/features upstream would just
issue cp -a $PINAX_ROOT/apps/blog
$PROJECT_ROOT/apps and be done. Those
who consider themselves good
FLOSS-country citizens however care
about contributing back as well ...
The default pinax apps you would be looking to override (if necessary), would be:
http://pinaxproject.com/docs/dev/apps/account/
http://pinaxproject.com/docs/dev/apps/profiles/
You probably want to have a go at overriding the built-in Pinax applications, which is gone over in a little detail in this article. I imagine you'd want to extend (or override) Pinax's Profile model.
This chap seems to have been in a situation that sounds like what you want, have a quick read of his chat logs to see what I mean. Sorry that this answer isn't too specific, it's more of a pointer.