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/
Related
I don't know how to solve it. It's really frustrating. If you managed to help, I would be very grateful.
settings:
STATIC_URL = '/static/'
MEDIA_URL='Proyectoweb/media/'
MEDIA_ROOT=BASE_DIR / 'Proyecto/media'
url:
from django.urls import path
from ProyectoApp import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns+=static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
models:
from django.db import models
# Create your models here.
class Servicio(models.Model):
titulo=models.CharField(max_length=50)
contenido=models.CharField(max_length=50)
imagen=models.ImageField(upload_to='servicios')
created=models.DateTimeField(auto_now_add=True)
updated=models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name='servicio'
verbose_name_plural='servicios'
def __str__(self):
return self.titulo
admin:
from django.contrib import admin
from .models import Servicio
# Register your models here.
class ServicioAdmin(admin.ModelAdmin):
readonly_fields=('created', 'updated')
admin.site.register(Servicio, ServicioAdmin)
For some reason, this page asks me for more details, when I think I have already given enough.
I try to be able to load an image in the django admin, but I can't see it when I click....
I want to import export option in Django admin panel data import and export
https://django-import-export.readthedocs.io/en/stable/getting_started.html
also, try with this example
https://simpleisbetterthancomplex.com/packages/2016/08/11/django-import-export.html
python 3.7
django 2.2.5
django-import-export 1.2.0
settings.py
IMPORT_EXPORT_USE_TRANSACTIONS = True
models Person
resource.py
from import_export import resources
from .models import Person
class PersonResource(resources.ModelResource):
class Meta:
model = Person
admin.py
from import_export.admin import ImportExportModelAdmin
from django.contrib import admin
from import_export import resources
from .models import Person, Pravacy_check
#admin.register(Person)
class PersonAdmin(ImportExportModelAdmin):
pass
# Register your models here.
class PersonResource(resources.ModelResource):
class Meta:
model = Person
class PersonAdmin(ImportExportModelAdmin):
resource_class = PersonResource
admin.site.register(PersonAdmin)
all app modules data import and export need to work prefect.
Try this code
from django.contrib import admin
from .models import Person #
from import_export import resources
from import_export.admin import ImportExportModelAdmin
class PersonAdmin(ImportExportModelAdmin): # FOR ADMIN IMPORT EXPORT ONLY
pass
admin.site.register(Person, PersonAdmin) # FOR ADMIN IMPORT EXPORT ONLY
Settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'import_export',
'app',
]
I am getting following error and donĀ“t know how to solve this:
from . import views
ImportError: cannot import name views
This are my scripts i am using:
urls.py
from django.conf.urls import url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^$',views.home,name='index'),
url(r'^admin/', admin.site.urls),]
views.py
from __future__ import unicode_literals
from django.http import HttpResponse
def index(request):
return HttpResponse("libaray management system")
Try to import like this
from your_app_name import views
I can't say, what is your project structure and where are your views.py and urls.py files. My guess, that you have some problems with how you import (check the absolute and relative import in python). In that case you can use import style from above code example.
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
I have hundreds of routes in my flask main module,
I think it need to separate those hundred of routes from the main module.
How to do it ?
#!/usr/bin/env python3
# -*- coding: utf8 -*-
from flask import request, url_for
from flask import Flask, request, jsonify
from flask_request_params import bind_request_params
from flask import g
import datetime
import pandas as pd
import pymongo
from webargs import Arg
from webargs.flaskparser import use_args, use_kwargs
import yaml
import time, functools
from pdb import set_trace
from pandas_helper import PandasHelper
import errors
from app_helper import *
from release_schedule import ReleaseSchedule
from mongo import Mongo
#app.route('/next_release', methods=["GET"])
#return_json
def next_release():
schedules = ReleaseSchedule.next_release(DB)
return pd.DataFrame([sche for sche in schedules])
...
#app.route('/last_release', methods=["GET"])
This is what blueprints were made to do.
Another alternative is flask-classy (which is awesome). I'm going to talk about the blueprint approach since that's what I know better.
If I was in your position I would want to split my routes up based on common imports.
Without knowning your application I'm going to guess that a distribution like this
parse_user_data_views.py
from webargs import Arg
from webargs.flaskparser import use_args, use_kwargs
import yaml
push_to_db_views.py
from pandas_helper import PandasHelper
from mongo import Mongo
import pymongo
import pandas as pd
import datetime
release_views.py
from release_schedule import ReleaseSchedule
import pandas as pd
#app.route('/next_release', methods=["GET"])
#return_json
def next_release():
schedules = ReleaseSchedule.next_release(DB)
return pd.DataFrame([sche for sche in schedules])
is likely distribution. We can't answer this for you, only you can.
But this allows you to separate out your application in some pretty nice ways.
in __init__.py
from flask import Flask
from yourapplication.release_views import release_views
from yourapplication.push_to_db_views import push_to_db_views
from yourapplication.parse_user_data_views import parse_user_data_views
app = Flask(__name__)
app.register_blueprint(release_views)
app.register_blueprint(push_to_db_views)
app.register_blueprint(parse_user_data_views)
Create a new file called views.py and add all your routes there. Then import views.py in your __ init __.py .