I followed the django import-export manual but the Import Export buttons do not appear in my admin screen.
This is what I have in my admin.py. Is there anything else I need to do? I have added import-export to my settings.py.
from django.contrib import admin
from costtool import models as m
from costtool.models import UserProfile, Prices
from import_export import resources
from import_export.admin import ImportExportModelAdmin, ImportMixin
class PriceResource(resources.ModelResource):
class Meta:
model = Prices
class PriceAdmin(ImportExportModelAdmin):
resource_class = PriceResource
pass
admin.site.register(UserProfile)
admin.site.register(Prices)
Just tell the admin what ModelAdmin to use:
admin.site.register(Prices, PriceAdmin)
You can check Django's ModelAdmin doc and try to use the new register decorator if you're using Django 1.7
Related
I'm trying to manage my User table with django.contrib.admin
But during I add the User table in admin, I had an issue that doesn't appear in admin site.
Here is my code.
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
#admin.register(User)
class CustomUserAdmin(UserAdmin):
pass
assert admin.site.is_registered(User) # Fails here
And when I added the other custom model, it works.
Thanks
When you modify or implement the user model, you have to be aware of AUTH_USER_MODEL.
I changed AUTH_USER_MODEL in setting.py, and it starts to work.
Thanks.
I have model Product.
1) There is markdownx field (from django-markdownx package) and I want use markdown editor in admin
2) I use import-export in admin for this model.
I can make *import-export** works by using ImportExportModelAdmin:
from import_export.admin import ImportExportModelAdmin
class ProductAdmin(ImportExportModelAdmin): [some_code...]
admin.site.register(Product, ProductAdmin)
I can make markdownx works by using MarkdownxModelAdmin:
from markdownx.admin import MarkdownxModelAdmin
class ProductAdmin(MarkdownxModelAdmin): [some_code...]
admin.site.register(Product, ProductAdmin)
How to make them both works?
You can do it by inheriting multiple python classes as follows:
from markdownx.admin import MarkdownxModelAdmin
from import_export.admin import ImportExportModelAdmin
class ProductAdmin(MarkdownxModelAdmin):
[some_code...]
class ProductImportExport(ImportExportModelAdmin, ProductAdmin):
[some_code...]
admin.site.register(Product, ProductImportExport)
I have an app called profile which has de model Profile(models.Model). Then, I have another app called offer with the model Offer(models.Model) and it has almost all the attributes of the Profile one, so I wanted to include a form with the attributes of profile in the offer.
In the forms.py in the offer app, I have created:
from django.forms import ModelForm
from profile.models import Profile
from offer.models import Offer
class ProfileOfferForm(ModelForm):
#extrafields
class Meta:
model = Profile
exclude = ('min_salary',)
And then in the admin.py (in the app Offer too) I have:
from django.contrib import admin
from django import forms
from offer.models import *
from offer.forms import *
# Register your models here.
from profile.models import Profile
#admin.register(Offer)
#admin.site.register(Offer, OfferAdmin, ProfileForm)
class OfferAdmin(admin.ModelAdmin):
inlines = [
QuestionInline,
AutomatismInline,
CandidateInline
]
form = ProfileOfferForm
And I get no errors, the only problem is that de fields of Profile don't appear in the administration in the section Offer. Am Ileaving something?
Any help will be appreciated,
Thanks!
It looks like you forget to register your custom Admin to your Model.
# At the bottom do something like this
admin.site.register(Offer, OfferAdmin)
Has anyone seen this before? I've tried making new apps, projects, etc.
All thats in my admin.py file is:
from django.contrib import admin
from . models import UserProfile, Tribe, Membership
# Register your models here.
admin.site.register(Tribe)
admin.site.register(Membership)
admin.site.register(UserProfile)
I've not got any static files or css in the app..?
Create a class that inherit admin.ModelAdmin, update the fields to be shown in the list_display tuple, and register TribeAdmin instead of Tribe. Do the same for the rest.
from django.contrib import admin
from . models import UserProfile, Tribe, Membership
# Register your models here.
class TribeAdmin(admin.ModelAdmin):
list_display = ('field_1', 'field_2',)
admin.site.register(Tribe, TribeAdmin)
# admin.site.register(Membership)
# admin.site.register(UserProfile)
For all the available options, have a look at the documentation or an easy to understand beginner tutorial from the DjangoBook (please note its for an outdated Django Version, but fields works with Django 1.8)
With Django 1.8 you can use.
#admin.register(Tribe)
class TribeAdmin(admin.ModelAdmin):
list_display = ('field',)
I've got sorl-thumbnail up and running in templates with Redis to store the thumbnails. Great stuff!! However, I would like to have thumbails in my Admin. I used the example in the documentation (see below) but with no luck.
from gallery.models import Photo
from django.contrib import admin
from sorl.thumbnail.admin import AdminImageMixin
class PhotoAdmin(AdminImageMixin, admin.ModelAdmin):
pass
admin.site.register(Photo, PhotoAdmin)
What am I doing wrong?
I do something very similar and it works for me. However, I use a slightly different method, importing my admin from a utils/admin.py in my site base instead, allowing easy inheritance across my models with other apps such as django-reversion, django-guardian, and django-markitup.
gallery/admin.py:
#from django.contrib import admin
from utils import admin
from gallery.models import Photo
class PhotoAdmin(admin.ModelAdmin):
#your customizations
admin.site.register(Photo,PhotoAdmin)
utils/admin.py:
from django.contrib.admin import *
from django.db import models
from sorl.thumbnail.admin import AdminImageMixin
class ModelAdmin(AdminImageMixin, ModelAdmin):
pass
Your model's ImageFields need to be sorl's ImageField (from sorl.thumbnail.fields import ImageField) instead of the standard django.db.models.ImageField.
This field is a drop-in replacement, so just updating this should fix the issue, or at least it did for me. If you are using South for database migrations, note that it will generate one for this, which is fine.