I am trying to modify an existing School app which has sub-apps like students, class, scores etc using django 1.8.
My class model:
class Class(Object):
student = models.ForeignKey(School, related_name='school_student')
section = models.CharField()
roll = models.IntegerField()
When I run python manage.py makemigrations class, I am getting a message No changes detected in app 'class'.
But when I run python manage.py makemigrations, the changes are detected and the migration files are created under the school directory but not in the class directory.
But when to the above model if I add a meta class.
class Meta:
unique_together = ('student', 'roll')
And now if I run python manage.py makemigrations class, the changes are detected and the migration files are created under the class directory.
Can someone tell me why such behaviour?
First of all: A model is not a subapp.
If that's clear: IIRC, Django 1.8 requires a class Meta to know which app a model belongs to.
If you only have an additional model, you need class Meta to specify the app_label.
If you have a real subapp (i.e. an app within a app), you also need to add it to settings.INSTALLED_APPS for their migrations to work properly.
Related
I have a django project that was created on an Oracle database and I want to switch to ANOTHER Oracle database. I have followed this tutorial https://pythonfusion.com/switch-database-django/, but there is a problem that not all models are created initially in Django, some are created using inspectdb on existing tables in other databases . Therefore, when using the migrate --database=new command, I get errors about those tables that already existed before Django was created. Is there a way to migrate only the models and tables necessary for Django to work? (users, auth...)
I think you have to take a look at the managed attribute of each model meta class.
If managed is true then django will change the model in the database.
Unmanaged model :
class MyModel(models.Model):
...
class Meta:
managed = False # This means django will ignore MyModel when migrating
Managed model :
class MyManagedModel(models.Model):
...
class Meta:
managed = True # This means django will migrate MyManagedModel
More documentation here : https://docs.djangoproject.com/en/4.1/ref/models/options/
Yes you can definitely customize your migration behavior, the command python manage.py makemigrations creates a couple of files that are used to migrate your models into your DB, any who you can still access these files and choose exactly what to include, exclude and even edit them.
Check the following link:
https://dev.to/koladev/writing-custom-migrations-in-django-3eli
If I've understood your question correctly, then you're looking to use Django's built-in migrations. To find out which migrations have been run against your new database, run the command manage.py showmigrations --database=new which will show you a list of all migrations that exist within the context of your application.
Once that is done, you can manually run the desired migrations (e.g. auth and contenttypes) by running the command manage.py migrate --database=new app_label migration_name.
showmigrations command: https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-showmigrations
migrate command: https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-migrate
I'm trying to setup two related apps in a django project, where one app's model references the other in a foreign key relationship. This is normally a simple thing, but I'm running into some collisions with app names (one of my apps is called 'account' and that's not unique in my installation) and my subsequent fix to that by adding in specific name/label definitions in a custom AppConfig may also be contributing to the problem.
A little background on my environment:
Django 1.8.3
Python 2.7.6
Postgres 9.4.0
using this template for my project structure: https://github.com/pydanny/cookiecutter-django. Thanks pydanny!
I setup a simple project using this template to demonstrate the problem: https://github.com/rishikumar/django_test_model
Here are the relevant parts to the tree:
test/ (project app)
/config ()
/test (main django app)
/account
__init__.py
apps.py
models.py (Has a model called "Account")
/schedule
__init__.py
apps.py
models.py (Has a model called "Schedule" which has a ForeignKey relationship to the Account table)
Here is the first model class, under the test/account app
class Account(models.Model):
class Meta:
db_table = 'test_account'
After putting this in place, I added this project to the INSTALLED_APPS as "test.account". I ran makemigrations next.
./manage.py makemigrations test.account
I was expecting this to uniquely identify the account package underneath the test project, but its only looking for the "account" part of the name. (I couldn't find any documentation explaining why this is the case)
django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: account
To work around this problem I setup a custom AppConfig for this. in test/test/account/init.py I added the following:
default_app_config = 'test.account.apps.AppConfig'
And to test/test/account/apps.py I added:
class AppConfig(apps.AppConfig):
name = 'test.account'
label = 'test.account'
Now I can run make migrations as I originally intended and it works as expected.
./manage.py makemigrations test.account
Incidentally, I gave the model a specific table name because it was specifying "test.account_account" by default and that causes some headaches in the postgres CLI - I have to quote the entire table name to reference it.
I setup the second app the same way - added a default app config and setup the name and label of the app config to be "test.schedule"
Here is the models.py in the schedule app:
from test.account.models import Account
class Schedule(models.Model):
class Meta:
db_table = 'test_schedule'
account = models.ForeignKey(Account)
Now I run makemigrations on this app:
./manage.py makemigrations test.schedule
I get the following error:
ValueError: Lookup failed for model referenced by field test.schedule.Schedule.account: test.schedule.test.account.Account
Notice that the Account class that its trying to access says "test.schedule.test.account.Account" when it should referencing "test.account.Account"
I'm not sure if my solution for the AppConfig entry was the right choice - perhaps there is a better way to uniquely identify an app within the django installation?
I'm guessing that I'm making a bad decision about how to name things in my AppConfig, but I'm not sure how to reconcile the fact that I need to specify an AppConfig to avoid having duplicate application labels while still being able to avoid confusing the lookup for the foreign key reference that the migration tool is using.
In my django project I use django-registration reusable app. I install this app and run syncdb. It's create for me table 'registration_registrationprofiles' in my database. Then I create a new app and write this code in my models.py:
class Comments(models.Model):
text = models.TextField()
pub_date = models.DateTimeField(auto_now=True)
user = models.ForeignKey('registration_registrationprofiles')
And run manage.py makemigrations and it throw me exception:
ERRORS:
comments.Comments.user: (fields.E300) Field defines a relation
with model 'registration_registrationprofiles', which is either
not installed, or is abstract.
How I can fix this problem?
Try this:
from registration.models import RegistrationProfile
and then:
user = models.ForeignKey(RegistrationProfile)
Migrations can have dependencies declared. Usually, makemigrations does a good job with that, but it looks like it missed it this time. I suggest you locate the migration file it created (in your_app/migrations) and check its dependencies. It should look like this:
class Migration(migrations.Migration):
dependencies = [("registration", "0042_some_migration")]
# operations...
The key here it the dependencies array should reference the registration app, and the latest migration (or at least the latest you depend on).
Then manage.py makemigrations will detect the dependency and run migrations in the correct order.
I changed my former user model so that now it inherits from django's user model.
from django.contrib.auth.models import User
class UserProfile(User):
#fields..
but other models were pointing to my former model and now if i want to migrate, i am getting the error:
(user_id)=(9) does not exist in auth_user table.
reasonable error message. But what should i do now? i am really stuck. i am using django version 1.4
I made a screenshot of the error:
You don't say what version of Django you're using; if you're using 1.5, then you also need to set the AUTH_USER_MODEL setting to tell Django to use it (see the auth docs for more info). If you're on an earlier version, you probably don't want to subclass the User model at all, but create a profile (like your class name indicates) as a separate model and link it with a ForeignKey (see the old profile docs for more on that).
Did you also change the name of the model when you added the parent class? You probably want to set the name of the table in UserProfile so that it matches the old name. From the Django model docs:
To save you time, Django automatically derives the name of the database table from the name of your model class and the app that contains it. A model’s database table name is constructed by joining the model’s “app label” – the name you used in manage.py startapp – to the model’s class name, with an underscore between them.
For example, if you have an app bookstore (as created by manage.py startapp bookstore), a model defined as class Book will have a database table named bookstore_book.
To override the database table name, use the db_table parameter in class Meta.
So something like this would do the trick:
class UserProfile(User):
# other stuff
class Meta:
db_table = "myapp_user"
Hope this helps!
I am using Django 1.1.4 with python 2.6. I just added a new model to the list of models and used the syncdb command, but it's not showing in the database. I have already made sure I use 'my_app' name for app label in the Meta class. I also tried making a new database and syncing all the models again, but strangely my model is the only one not getting synced. Below I have included the code:
from django.db import models
from coredump.analyzer.models.model import ExtendedModel
class Netpath(ExtendedModel):
buildno=models.IntegerField()
path = models.CharField(max_length=300)
class Meta:
app_label = 'analyzer'
ordering = ['buildno']
def __unicode__(self):
return '%d' % self.buildno
Extended Model is another model that I have made that works perfectly fine with the other models in the framework.Adding it's code as well won't be that useful.
Any help will be greatly appreciated
syncdb will only create tables for models found in <app>.models. Import the model class there if necessary.
did your new model was on a new apps ? in this case, don't forget to add the new apps in INSTALLED_APPS settings...
in fact, django is absolutely not mysterious or magic.