Opened 8 years ago
Closed 8 years ago
#28098 closed Bug (invalid)
Error while doing migrations for a new model that is used in an form in the admin.py
Reported by: | GHARIANI Mohamed | Owned by: | nobody |
---|---|---|---|
Component: | Migrations | Version: | 1.8 |
Severity: | Normal | Keywords: | migrations |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I tried to apply migrations for a new model. The model was used in a form used in the admin so the migrations failed.
He is a sample code i wrote which gave me the same errors.
models.py
from django.db import models class App(models.Model): name = models.CharField(max_length=50)
forms.py
from django import forms from app.models import App class AppForm(forms.ModelForm): name = ((elem.id, elem.name ) for elem in App.objects.all())
admin.py
from django.contrib import admin from app.forms import AppForm from app.models import App class AppAdmin(admin.ModelAdmin): search_fields = ('id', 'name') form = AppForm list_display = ('id', 'name') admin.site.register(App, AppAdmin)
While running ./manage.py makemigrations i got this error
django.db.utils.OperationalError: no such table: app_app
Note:
See TracTickets
for help on using tickets.
Unfortunately your modelform makes no sense:
If you've followed advice from someone or read that somewhere stop using such help.
And if that's code you created by yourself then please read the modelform documentation: https://docs.djangoproject.com/en/1.8/topics/forms/modelforms/
Django modelforms (like other components ike models, etc.) are defined in a declarative style.
It makes no sense trying to define something in a declarative way by looping over a ORM queryset (DB query) like you are trying to do.
When you see a model or modelform definition think it's similar to an object or data structure definition which when using a static, compiled programming language (like C++, etc.) define its shape and behavior.
Then when your Django app is running, these definitions are used together with your database and the information it contains. But this another latter, stage (thing runtime of a program as opposed to the compile time described above).
This is why doesn't make sense to use a DB query when defining the modelform. You are trying to use data from the runtime stage at the modelform declaration stage.