I need a list of django models registered, Just like django admin index page. I'm using django 2.0 where from django.contrib.admin.validation import validate and from django.db.models import get_models is outdated. Advance thanks.
from django.apps import apps
apps.get_models()
https://docs.djangoproject.com/en/dev/ref/applications/#django.apps.AppConfig.get_models
For a particular application:
from django.apps import apps
apps.all_models['<app_name>']
https://stackoverflow.com/a/1126209/196834
from django.contrib.admin.validation import validate
It was deprecated.
https://docs.djangoproject.com/en/2.0/releases/1.7/#modeladmin-validators
Related
I am developing an app in Django.
I have a model named glossary_entry and I want to be able to use the import_export widget for it (see image for example).
So I have read the docs and acted as follows:
I have already run
pip install django-import-export
added to settings.py
INSTALLED_APPS = [
'import_export',
run:
pip freeze>requirements.txt
And in my admin.py I have:
from django.contrib import admin
from .models import glossary_entry
from import_export import resources
from import_export.admin import ImportExportModelAdmin
class glossary_entry_resource(resources.ModelResource):
class Meta:
model=glossary_entry
# Register your models here.
admin.site.register(glossary_entry)
The problem is that when I run the server, I get
Connection negated by 127.0.0.1
But even before that, in my VS code editor, i get an error underlined at lines
from import_export import resources
from import_export.admin import ImportExportModelAdmin
which tells:
Unable to import 'import_export'pylint(import-error)
What am I missing?
Try this code
from django.contrib import admin
from .models import glossary_entry
from import_export import resources
from import_export.admin import ImportExportModelAdmin
class glossary_entryAdmin(ImportExportModelAdmin): # FOR ADMIN IMPORT EXPORT ONLY
pass
admin.site.register(glossary_entry, glossary_entryAdmin) # FOR ADMIN IMPORT EXPORT ONLY
And go through docs https://django-import-export.readthedocs.io/en/latest/
I am trying to import my models into views.py but I am unable to do so. However I am able to register them on the admin site but when I use the same code I used in admin.py to import the models into views.py, I get an error. I am using djongo so I am not sure if that changes anything about how to import them and I cannot seem to find the documentation for it.
models.py
from djongo import models
class Round(models.Model):
round_num = models.IntegerField(default=0)
admin.py
from django.contrib import admin
from .models import Round
admin.site.register(Round)
views.py
from .models import Round
When I try and run my views.py file I get the following error: ModuleNotFoundError: No module named 'main.models'; 'main' is not a package
Also my views, admin, and models file are all in the same directory. I have made the migrations and I can see my Round model in MongoDB. The only thing I cannot do is import it to the view
You need to have an __init__.py file in your directory. It should be inside of your main folder and at the same level as your views.py and models.py
As a workaround, since the models properly migrate to MongoDB. Using pymongo I have just connected to Mongo and have rendered data into my views this way. It works fine so if anybody else has an issue loading in their models, you can always just connect directly to the DB.
Version Info:
Python 3.4, Django 1.8, Oscar Commerce - VERSION = (1, 2, 1, 'final')
I am trying to customize Products in the catalogue app following the documentation.
Having forked the catalogue app, I have defined models.py as follows:
from django.db import models
from oscar.apps.catalogue.abstract_models import AbstractProduct
class Product(AbstractProduct):
is_active = models.BooleanField(default=False)
from oscar.apps.catalogue.models import *
I have already included the modified catalogue, in the INSTALLED_APPS in settings.py as a list, as suggested for a similar problem here.
INSTALLED_APPS = INSTALLED_APPS + get_core_apps(
['app.gravytrain.catalogue',])
Have copied the migration folder from oscar/apps/catalogue to my custom app.
However running migration causes the following error:
RuntimeError: Conflicting 'product_product_options' models in
application 'catalogue': <class
'gravytrain.catalogue.models.Product_product_options'> and <class
app.gravytrain.catalogue.models.Product_product_options'>.
How do I get over this error ?
If you want to import some models, you need use get_model function.
For example:
from oscar.core.loading import get_model
Product = get_model('catalogue', 'Product')
I had the same error. I have also included "from oscar.apps.catalogue.models import *" in the top of the model. Once I was removed it, that issue fixed.
I need to add my model to my admin site on django 1.9,
I was using this code earlier:
for model in get_model(get_app('campaign')):
admin.site.register(model)
However, it isn't supported by django 1.9 verison. Is there a new way to do this?
You can use the get_models() method of the app config.
from django.contrib import admin
from django.apps import apps
myapp = apps.get_app_config('myapp')
for model in myapp.get_models():
admin.site.register(model)
In modern versions of Django (I'm not sure whether it will work for Django < 1.10), you can simplify this to:
myapp = apps.get_app_config('myapp')
admin.site.register(myapp.get_models())
I have a problem buliding django on jython
I have already installed django-jython , jython , django
in settings.py dababase engine I write : doj.db.backends.sqlite
raiseImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured:
'doj.db.backends.sqlite' isn't an
available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
u'base', u'mysql', u'oracle', u'postgresql_psycopg2', u'sqlite3'
Error was: cannot import name BaseDatabaseWrapper
it seems that in doj.db there is no these classes which I can find in django.db
and I find in site-packages\django_jython-1.7.0b2-py2.7.egg\doj\db\backends\sqlite\base.py
there are :
from doj.db.backends import JDBCBaseDatabaseWrapper as BaseDatabaseWrapper
from doj.db.backends import JDBCBaseDatabaseFeatures as BaseDatabaseFeatures
from doj.db.backends import JDBCBaseDatabaseOperations as BaseDatabaseOperations
from doj.db.backends import JDBCCursorWrapper as CursorWrapper
from doj.db.backends import JDBCConnection
maybe the problem lies here
thanks for your help
I got the same problem. It's Django compatibility issue indeed. django-jython 1.7 works with Django 1.7.x (see https://pythonhosted.org/django-jython/release-notes.html#b2)