Django admin, Use ImportExportModelAdmin and MarkdownxModelAdmin (Multiple) - django

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)

Related

How can I add inlines to the ModelAdmin of another app, without a circular dependency?

Let's say I have two models, in different apps. App Two knows about app One, but not the other way around:
# one/models.py
from django.db import models
class One(models.Model):
pass
# two/models.py
from django.db import models
from one.models import One
class Two(models.Model):
one = models.ForeignKey(One)
I also have One registered in the admin site:
# one/admin.py
from django.contrib import admin
from .models import One
admin.site.register(One)
How do I register Two as an Inline on One's admin page, without introducing a circular dependency between the two apps?
You can do this pretty simply, providing you don't mind accessing a 'private' attribute on the ModelAdmin. (Attributes beginning with an underscore are treated as private by convention.)
# two/admin.py
from django.contrib import admin
from one.models import One
from .models import Two
class TwoInline(admin.StackedInline):
model = Two
admin.site._registry[One].inlines.append(TwoInline)
I had the same issue but solved it more gentle way.
# one/admin.py
class OneAdmin(admin.ModelAdmin):
model = One
admin.site.register(One, OneAdmin)
# two/admin.py
class TwoInline(admin.TabularInline):
model = Two
import one.admin
class OneAdmin(one.admin.OneAdmin):
inlines = [TwoInline]
admin.site.unregister(One)
admin.site.register(One, OneAdmin)
As you see I extended the original ModelAdmin from first app and added inlines from second app. Don't forget to unregister the model from first app before registering it again.
It's safe and much better than to access private member of the class as was suggested.
I would try the following:
# one/admin.py
from django.contrib import admin
from one.models import One
from two.models import Two
class TwoInline(admin.StackedInline):
model = Two
class OneAdmin(admin.ModelAdmin):
inlines = [TwoInline,]
admin.site.register(One, OneAdmin)
You can read more at the docs.

ModelForm not showing in admin

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)

Django import-export buttons DO NOT appear in admin screen

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

Django - using Many-to-Many horizontal interface outside of admin

I'm working in a form with a m2m field. I want that this field looks like the horizontal interface of the django admin site... ¿how i can do it?
thanks...
You need to use the FilteredSelectMultiple widget
from django.contrib.admin.widgets import FilteredSelectMultiple
from django import forms
from .models import Person
class PersonForm(forms.ModelForm):
some_field = forms.ModelMultipleChoiceField(Person.objects.all(), widget=FilteredSelectMultiple("Person", False, attrs={'rows':'2'}))
class Meta:
model = Person
You will also need to include the Javascript and CSS used in the admin. Here's an example

Can someone explain me how to get sorl-thumbnail working on the Admin page of Django?

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.