models.py not getting generated when using the inspectdb - python

I am trying to generate models.py from a legacy database using the inspectdb command. Everything was fine, i see the table names being inspected and corresonding classes made in the console, after that operation is completed, I checked the directory where the manage.py is residing, and there was no models.py generated.
Can anyone throw some light on this?
Thanks,
John

The inspectdb command does not automatically create a models.py file, by default it outputs the models to the console.
You can save the output to a file on Unix systems with
$ python manage.py inspectdb > models.py
Note this will overwrite models.py if it already exists!

Related

python manage.py makemigrations : No changes detected

I am working on a project using django and I am using pycharm software. In my 'store' directory i have a python package called 'models' in which there are two python files :- init.py and product.py . The problem i am getting is that I cannot create table using the command even when I am importing the 'Product' class from product module in init.py file. Even both the modules are also in the same package.
Here is the code of the product.py file:-
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=50)
price = models.IntegerField(default=0)
description = models.CharField(max_length=200, default='')
image = models.ImageField(upload_to='products/')
And here is the code of init.py file:-
from .product import Product
But when i am running the command on terminal i am getting this error:-
(venv) C:\Users\Admin\PycharmProjects\SMart>python manage.py makemigrations
No changes detected
You must put the name of your app <store> into the list of INSTALLED_APPS inside your settings.
I had the same issue with Django 4. To solve it, I created migrations/__init__.py file in every app directory. So just run these command for every app:
mkdir appname/migrations
touch appname/migrations/__init__.py
Then use manage.py:
python3 manage.py makemigrations
python3 manage.py migrate
And it worked.

Django: no such table snippets_snippet

I'm following this Django Rest tutorial on serialization: http://www.django-rest-framework.org/tutorial/1-serialization/#getting-started
I followed it pretty much to the letter. It gives the above error when I try to save a snippet.
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
snippet = Snippet(code='foo = "bar"\n')
snippet.save()
I'm working on Windows. The tutorial is made for Apple. I have had to enter some commands slightly differently for this reason. I have no idea if this has to do with what is wrong in this case.
I don't know where to even start in figuring out the problem here, so I could use any help. Thanks.
You need to make the migration (ie: forcing the db to corespond to what is defined by your new python code)
Do the following:
python manage.py makemigrations snippets
python manage.py migrate
You should remove db.sqlite3 as well as snippets/migrations. Later, create a folder named migrations inside snippets folder and create a __init__.py file inside snippets/migrations/ folder. Finally run:
python manage.py makemigrations && python manage.py migrate
Be sure you are into the virtual environment with the correct python version.
I believe the error refers to the models.py. Could you show the file so I can double check that too. Also there is a chance for unapllied migartions, double check if you've done this as well.

Django 1.7 - makemigrations not detecting changes - managed models

I have just installed django 1.7 in my virtual env.
Then I manually created the following files:
service_bus/
service_bus/__init__.py
service_bus/django_settings.py
service_bus/models
service_bus/models/__init__.py
service_bus/models/dsp.py
service_bus/models/audience_type.py
service_bus/models/category.py
service_bus/models/audience.py
service_bus/models/dsp_config.py
service_bus/models/apsettings.py
So I have a settings file service_bus/django_settings.py and the service_bus app.
Then I did, on bash:
export DJANGO_SETTINGS_MODULE='service_bus.django_settings'
Then I just try to run makemigrations, but it says no changes are detected.
$ django-admin makemigrations
Loading properties from /etc/s1mbi0se/dmp.ini
System check identified some issues:
WARNINGS:
?: (1_6.W001) Some project unittests may not execute as expected.
HINT: Django 1.6 introduced a new default test runner. It looks like this project was generated using Django 1.5 or earlier. You should ensure your tests are all running & behaving as expected. See https://docs.djangoproject.com/en/dev/releases/1.6/#new-test-runner for more information.
No changes detected
$ django-admin makemigrations service_bus
Loading properties from /etc/s1mbi0se/dmp.ini
System check identified some issues:
WARNINGS:
?: (1_6.W001) Some project unittests may not execute as expected.
HINT: Django 1.6 introduced a new default test runner. It looks like this project was generated using Django 1.5 or earlier. You should ensure your tests are all running & behaving as expected. See https://docs.djangoproject.com/en/dev/releases/1.6/#new-test-runner for more information.
No changes detected in app 'service_bus'
In all my models I have something like
class APSettings(models.Model):
...
class Meta:
db_table = u'APSettings'
app_label = 'service_bus'
What could I be missing?
You need to run the migrate command first to scaffold the database schema. Then, you can run makemigrations for each app. Check the Django tutorial for more on this.
Make sure you update your models.py file to actually import the models. For example in models.py you'd have from service_bus.models.audience import *. The manage script goes over that file, imports all the models in audience.py and detects changes in there. If you did not add your new models to models.py then the manage script won't know about the new models in you models files.

no admin.py file in new django app

I'm new to Django and i've already come across a problem. I'm using Django 1.4.3 on OSX Mountain lion.
When I start a new app using
django-admin.py startapp "name"
the app is created and all the necessary files are within it (__Init__.py, models.py, tests.py, views.py). However, the admin.py file which should be automatically created is not in the app folder. Without it, i cannot edit my administrator site preferences.
Any ideas as to why this may be happening?
You have to create the file manually since the django admin is disabled by default.
Instructions on what to put in admin.py are here.
You are most likely reading the dev tutorial but using a stable release (currently 1.4.3). admin.py is created by startapp as of this commit which also updated the tutorial documentation but it won't make it into a stable release until 1.6.
Admin.py is not generated. In response to OP's to your comment on the post above:
From part 1 of the tutorial: https://docs.djangoproject.com/en/1.4/intro/tutorial01/
Let’s look at what startproject created:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
And on part 2, under the section "Make the poll app modifiable in the admin" it says:
But where’s our poll app? It’s not displayed on the admin index page.
Just one thing to do: We need to tell the admin that Poll objects have an admin interface. To do this, create a file called admin.py in your polls directory, and edit it to look like this:
from polls.models import Poll
from django.contrib import admin
admin.site.register(Poll)

Trouble running example django code

Im trying to learn Django by looking at examples, but Im having a bit of a problem running the examples I find.
I downloaded 'cheeserator' from https://github.com/jacobian/cheeserater
and I tried running it with python manage.py runserver
but I get the following error -
Error: Can't find the file
'settings.py' in the directory
containing 'manage.py'. It appears
you've customized things. You'll have
to run django-admin.py, passing it
your settings module. (If the file
settings.py does indeed exist, it's
causing an ImportError somehow.)
What am I doing wrong?
You need to have a settings.py file.
As per the instructions in the link provided:
Then you'll want to create a settings.py file in this directory containing::
from settings_template import *
# Override any settings you like here.
Or if you don't want to override anything rename settings_template.py to settings.py

Categories