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

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

Related

Render a select box as radios buttons in Django Admin

I want to show select box options as raido buttons in Django Admin change model view. I don't want to write custom model forms. I'm looking for a way to render some select boxes as radio buttons while keeping auto generated model forms of the Django admin. I'm using django v 1.11.
Assuming my_field is the field, we want to be rendered as Radio Button
# admin.py
from django.contrib import admin
from django import forms
from .models import MyModel
class MyModelAdminForm(forms.ModelForm):
class Meta:
model = MyModel
exclude = ()
widgets = {'my_field': forms.RadioSelect}
class MyModelAdmin(admin.ModelAdmin):
form = MyModelAdminForm
admin.site.register(MyModel, MyModelAdmin)

Django: Search many to many field while creating object

I've got a use case where I have multiple Ingredient that can be linked to a Recipe through the Django admin. Now I have around a hundred ingredients which makes it very difficult to select the ingredients in the following UI.
Is there a way to add a search field or something similar to the django admin for easier selection?
You have few choices.
1. filter_horizontal
With filter_horizontal, you can use horizontal m2m ui in admin. I prefer this way using m2m in admin.
class YourAdmin(admin.ModelAdmin):
filter_horizontal = ('m2m_field',)
...
And the result will be...
2. raw_id_fields docs
You can use raw_id_fields for using pop-up modal with your m2m fields.
It's bit useful when you have lots of m2m field. Also, it's easy to filter which m2m obj to add.
class YourAdmin(admin.ModelAdmin):
raw_id_fiedls = ('m2m_field',)
...
I suppose you want to filter over ingredients and select it one by one on admin UI
You can use django forms builtin CheckboxSelectMultiple
widget in place of SelectMultiple to make selection easy
from django import forms
from django.contrib import admin
class RecipeForm(forms.ModelForm):
class Meta(object):
model = Recipe
widgets = {
'Ingredient': forms.CheckboxSelectMultiple,
}
class RecipeAdmin(admin.ModelAdmin):
form = RecipeForm
admin.site.register(Recipe, RecipeAdmin)
Alternatively, you can use django-better-filter-widget
package if you want a search input on choices, Refer Github repo for
installation
It is a custom widget, created by overriding SelectMultiple widget of
django forms
from django import forms
from django.contrib import admin
from better_filter_widget import BetterFilterWidget
class RecipeForm(forms.ModelForm):
class Meta(object):
model = Recipe
widgets = {
'Ingredient': BetterFilterWidget(),
}
class RecipeAdmin(admin.ModelAdmin):
form = RecipeForm
admin.site.register(Recipe, RecipeAdmin)

How to change the table on admin site?

I'm new to Django and I would like to change the table that is on the admin site.
Now there is an User class and a Group class, I would like to add my own class, 'Games', to the admin site which have the variables 'name' and 'size'.
The class 'Games' is in my models.py.
How do I do this?
Add this to your admin.py file
from django.contrib import admin
from .models import *
class Gamesadmin(admin.ModelAdmin):
class Meta:
model = Games
fields = ('name','size',)
admin.site.register(Games,Gamesadmin)
Do not forget to use your class name correctly . Good Luck.

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 - Joining LogEntry to actual models

so i'm using the admin LogEntry object/table to log events in my app. I have a view where i'd like to display each LogEntry.
It would be really great if i could join the LogEntry with the actual objects they represent (so i can display attributes of the object inline with the log entry)
In theory this should be easy as we have the model type and id from the LogEntry but i can't figure out how to join them using a queryset.
i thought i could just grab all the ids of the different objects and make another dictionary for each object type and then join them somehow (maybe zip the lists together?) but that seems dumb and not very djano-ish/pythonic.
does anybody have better suggestions?
** edit **
just want to clarify am not looking to use admin, but roll a custom view and template.
As I know Django uses contenttypes framework to perform logging in admin. So you should create generic relation inside your model and then to show inlines in admin use GenericTabularInline and GenericStackedInline. Please consult with the article.
from django.contrib import admin
from django.contrib.admin.models import LogEntry
from django.contrib.contenttypes.generic import GenericTabularInline
from django import forms
from some_app import models
from some_app.models import Item
class LogForm(forms.ModelForm):
class Meta:
model = LogEntry
class LogInline(GenericTabularInline):
ct_field = 'content_type'
ct_fk_field = 'object_id'
model = LogEntry
extra = 0
class ItemForm(forms.ModelForm):
class Meta:
model = Item
class ItemAdmin(admin.ModelAdmin):
form = ItemForm
inlines = [LogInline,]
admin.site.register(models.Item, ItemAdmin)
and you add to Item:
class Item(models.Model):
name = models.CharField(max_length=100)
logs = generic.GenericRelation(LogEntry)
this change won't create anything in your database, so there is no need to sync
Recent Django versions require to create a proxy for LogEntry:
from django.contrib import admin
from django.contrib.admin.models import LogEntry
from django.contrib.contenttypes.generic import GenericTabularInline
class LogEntryProxy(LogEntry):
content_object = GenericForeignKey('content_type', 'object_id')
class Meta:
proxy = True
class LogInline(GenericTabularInline):
model = LogEntry
extra = 0
class ItemAdmin(admin.ModelAdmin):
inlines = [LogInline,]
admin.site.register(models.Item, ItemAdmin)