Hello fellow programmers!
I am reading about the functionality of the interactive shell reached through running ...python manage.py shell. This is something that I have done many times before, however I am not quite sure exactly How the actual shell connects with Django. Before asking this question, I have indeed tried to find my way through the official documentation - as well as other posts on this website. This question does not appear to have been presented with an answer a beginner like me can comprehend.
I have read that the manage.py shell-command 'gives us an interactive shell with the Python path set up correctly for Django.'. I know that it can be used to interact with a Django project, and to query configured databases and models - however what happens behind the scenes?
For example, when writing ModelName.objects.all() - what is the origin of .objects and .all(), because it does not seem to be pure python syntax? How does Django understand this?
Is the shell a special python-version provided by Django, with some added syntax that Django understands? Because that would make sense.
Thank you from Sweden!
From what I know, objects comes from the model having a manager.
From the docs:
A Manager is the interface through which database query operations are provided to Django models. At least one Manager exists for every model in a Django application.
Managers exist in django. So when you run the shell, you're just executing python code inside an environment that has django installed. You probably noticed that you have to run from .models import myModel, which means that having models (i.e. django code) isn't inherit to the shell, you need to manually import it.
A shell is just an interactive way of executing code. The only special thing from running it from manage.py is that it will have django and the rest of your packages in its PYTHONPATH.
Related
I have an old project running (Django 1.6.5, Python 2.7) live for several years. I have to make some changes and have set up a working development environment with all the right django and python requirements (packages, versions, etc.)
Everything is running fine, except when I am trying to make changes inside the admin panel. I can log on fine and looking at the database (sqlite3) I see my user has superuser privileges. However django says "You have no permissions to change anything" and thus not even displaying any of the models registered for the admin interface.
I am using the same database that is running on the live server. There I have no issues at all (Live server also running in development mode with DEBUG=True has no issues) -> I can only see the history (My Change Log) - Nothing else
I have also created a new superuser - but same problem here.
I'd appreciate any pointers (Maybe how to debug this?)
Finally, I found the issue:
admin.autodiscover()
was commented out in the project's urls.py for some reason. (I may have done that trying to get the project to work in a more recent version of django) - So admin.site.register was never called and the app_dict never filled. index.html template of django.contrib.admin then returns
You don't have permission to edit anything.
or it's equivalent translation (which I find confusing, given that the permissions are correct, only no models were added to the admin dictionary.
I hope this may help anyone running into a similar problem
Inside my Django project, I want to fire up a python shell wherein I can make simple changes to my database (saving new records, updating old ones, etc.) which are then automatically reverted back when I exit the shell. Essentially the same functionality as running:
rails console --sandbox
in a Ruby on Rails project. I'm not looking for all the features of a traditional sandbox such as blocking web-service calls, just the basic database rollback. Is this--or something similar--possible in Django?
I want to use this for testing purposes. I know I can use Django's testing infrastructure to do this, but there are some basic instances where this would be very useful (e.g. quickly confirming that a specific model functions the way I expect it to).
This is mostly done in python manage.py shell (or with django_extensions in INSTALLED_APPS better ... shell_plus) which is python command line from which you can manipulate the data and test some features you don't wan't to program before you test. Rollback in django shell is not possible as I know, but you can have separate settings.py with different DB settings so you will have another database where you can experiment in shell without corrupting your data.
Here some sources:
Django Best Practice: Settings file for multiple environments
Django shell (from DJango docs)
As the title states, I am curious what I need to know to make a website with Django.
My Attempts:
I am familiar with Python, but despite my attempts to begin work on a webpage(attempted some Django tutorials online and purchased "2 Scoops of Django" and started to work with some of its recommendations) I always feel like it points me at something else to learn. (PostgreSQL, git, virtualenv, VirtualBox, Vagrant, and more.) I understand that some of these are tools I just need to implement, but I feel as if I could delve into these much further and don't understand when I should stop trying to learn more about these.
My Goal:
I want to be able to develop a webpage with Django, and understand the steps and tools I am implementing.
My Question:
What tools do I need to learn, and how much about them do I need to learn to be able to begin working effectively with Django?
This is a very broad question but I can try to answer it as clearly as possible.
You said you are familiar with Python, that's a good thing. The next thing you should know is the MVC framework that Django is based on and uses extensively.
You can refer to Django tutorial here: https://docs.djangoproject.com/en/1.9/intro/tutorial01/ (as already mentioned)
I can give you a TLDR of how this can work:
1. Create a django project : $ django-admin startproject mysite
2. In the file structure that is created, the most important things would be:
a. models.py - your database models or schema defined as classes and objects
b. views.py - the view you are trying to display (mainly rendering .html in your case)
c. settings.py - you path, app setting, permissions, etc
d. urls.py - how you will be calling your specific views (redirect urls)
Once you write a basic app, try to run it using $python manage.py runserver and voila!
For the website part, there a few easy ways. You can download twitter bootstrap and try to attempt a simple page that you can find online with django http://getbootstrap.com/2.3.2/
As far as technologies go:
venv: is so that you do not mess up your other python, etc versions on your laptop, it isn't necessary
git: this is something you should learn irrespective of a project requirement. There are basic 3 commands that will be enough.
You might have to learn basics of HTML and CSS for manipulating your own website. Most of the backend can be handled on Django using objects of models you created.
Try these things out and let me know if you need anymore information.
In Rails I can create a custom a job for rake which does whatever I want to and then run it as "rake my_task". What's a way to do it in Django?
In particular, I need to create such a job which read my own data files in a custom format, processes the data in a special way and them inserts it into the db. And I'm going to run it multiple times, not only once. To achieve that I created a pure python script and ran it but got an error
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
So a python script didn't work, there must be a django-way to do it.
The "Django Way" would be to create a custom command.
If you want to create an external python script, then there is also a way to do that explained in this stack overflow answer (EDIT: this is actually outdated for django 1.7) along with an example of how to create custom commands. I recommend the django documentation though if you're using the latest version of django.
On a related subject, check out fabric.
Edit:
For a django >= 1.7 standalone script:
import os
import django
from myapp import models
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
django.setup()
print models.MyModel.objects.get(pk=1)
There are 2 ways to do it on top of my head
1 - You can write a custom management command to be invoked by manage.py: https://docs.djangoproject.com/en/1.8/howto/custom-management-commands/
2 - The rake equivalence in python world is pyinvoke: https://github.com/pyinvoke/invoke, which is a successor of fabric that takes some design inspiration from rake itself.
When using invoke, if you need the django environment to be setup, use this https://docs.djangoproject.com/en/dev/ref/applications/#django.setup
I recently updated to appengine SDK 1.6 and I'm having trouble with persisting data on my dev environment. I have everything setup according to the official installation guide.
Even though I had a super user account setup for my app previously, it no longer works. I created the account again with
manage.py createsuperuser
and it seems to do what it's supposed to but the admin login doesn't work. Also, every time I run
manage.py syncdb
The script keeps informing me that I just install the auth system and don't have any users yet. Then it prompts me to create a super user.
Because nothing is persisting, I can't login to the admin page and any data I attempt to save using the built in shell doesn't keep either.
is it possible that you fire up a new issue at the issue tracker https://github.com/django-nonrel/djangoappengine/issues or at the mailing list http://groups.google.com/group/django-non-relational ? I think there might be a bug somewhere.
Depending on what you upgraded from, it's possible that you previously had no 'default partition' value - earlier versions didn't set one by default, but recent ones use 'dev:'. Try giving a --default_partition argument to manage.py.
I didn't end up changing anything but after trying it again on my Win 7 machine the next day, the issue wasn't there so it unfortunately isn't reproducible right now. Maybe logging out and logging back in changed some type of state after the install, that's my best guess. Sorry that I don't have any further information.