Django Admin: add inlines dynamically - python

class MyTemplateAdmin(admin.ModelAdmin):
list_display = ('name')
search_fields = ['name']
inlines = [
Template1Inline,
Template2Inline,
Template3Inline,
]
This works fine. But what I need is to make it dynamic. Whenever the admin adds a new Template to the MyTemplate Model, that needs to be added to the inlines.
Is there a way to do this? Please comment if I am not clear enough on my question.
Thanks in advance!

Just Override the admin's get_inline_instances.
def get_inline_instances(self, request, obj=None):
_inlines = super().get_inline_instances(request, obj=None)
custom_inline = YourDynamicInline(self.model, self.admin_site)
_inlines.append(custom_inline)
return _inlines

I haven't tested this, but in theory you could do:
class MyTemplateAdmin(admin.ModelAdmin):
def __init__(self, *args, **kwargs):
super(MyTemplateAdmin, self).__init__(*args, **kwargs)
#see if there are new templates
#and set the inlines list property
list_display = ('name')
search_fields = ['name']
Hope that helps you out.

In admin.py for the Templates:
class Template1Inline(admin.TabularInline)
pass
class Template2Inline(admin.TabularInline)
pass
Then in the admin.py for MyTemplateAdmin:
import sys, inspect, Templates.admin
class MyTemplateAdmin(admin.ModelAdmin):
list_display = ('name')
search_fields = ['name']
def __init__(self, *args, **kwargs):
inlines = [class_type[1] for class_type in inspect.getmembers(Templates.admin, inspect.isclass)]
super(MyTemplateAdmin, self).__init__(*args, **kwargs)
Templates.admin may not be correct depending on how you have your project setup, but the point is you just import the module that has the Template1Inline classes.

Just a quick idea.
from django.contrib import admin
from mymodule import signals
class MyModuleAdmin(admin.ModelAdmin):
def add_view(self, *args, **kwargs):
signals.my_dynamic_inline_hook_signal.send(
sender = self,
inlines = self.inlines,
args = args,
kwargs = kwargs
)

I'm not completely sure this is what you are looking for. You want inlines that are different instances of the same model?
One way of creating the inlines dynamically is with type() and adding them in get_inline_instances()
class MyTemplateAdmin(admin.ModelAdmin):
list_display = ('name')
search_fields = ['name']
inlines = [some_other_inline]
def get_inline_instances(self, request, obj=None):
new_inlines = []
for inline in (Template, Template2,Template3, ...): # I don't know how you want to get these.
attrs = {
'model': MyTemplate,
# 'extra': 1, Add extra attributes
}
# Create a new uninstanciated inline class
new_inlines.append(type('{}Inline'.format(inline),
(admin.TabularInline, ), attrs))
# Add the dynamically created inlines along with the ordinary.
self.inlines = self.inlines + new_inlines
return super(CommunityAdmin, self).get_inline_instances(request, obj)

Related

why Django admin short_description function not working?

I am trying to make some changes in my django admin panel such as want to show "title" instead of "blog_tile" but I am not understanding why changes not reflecting.
class BlogAdmin(admin.ModelAdmin):
readonly_fields = ['blog_publish_time', 'blog_update_time']
list_display = ['blog_title', 'blog_status',
'blog_publish_time', 'blog_update_time']
def rename_blog_title(self, obj):
return obj.blog_title[:10]
rename_blog_title.short_description = "title"
admin.site.register(Blog, BlogAdmin)
where I am doing mistake?
You are using blog_title, not rename_blog_title in your list_display. You thus should refer to the method, not to the field of your Blog model:
class BlogAdmin(admin.ModelAdmin):
readonly_fields = ['blog_publish_time', 'blog_update_time']
list_display = ['rename_blog_title', 'blog_status', 'blog_publish_time', 'blog_update_time']
def rename_blog_title(self, obj):
return obj.blog_title[:10]
rename_blog_title.short_description = 'title'
admin.site.register(Blog, BlogAdmin)

How to remove fields from CreateView depending on the user in Django?

I created a CBV of which I want to remove one or more fields, depending on the user. The idea is a jobsite and if the logged in user is a recruiter, than the employer field should be included, otherwise it should be excluded.
forms.py
class JobCreationForm(forms.ModelForm):
class Meta:
model = Job
# exclude = ['posted', 'provider', 'ext_id']
fields = ('title',
'job_desc',
'agency_name',
'employer',
'contact_name',
)
views.py
class JobCreateView(LoginRequiredMixin, CreateView):
template_name = 'job/job.html'
form_class = JobCreationForm
success_url = '/'
def get_context_data(self, **kwargs):
context = super(JobCreateView, self).get_context_data(**kwargs)
# import the Customers of this Company
self.fields["agency_name"].remove()
recruiter = self.request.user
self.fields["contact_name"].queryset = Profile.objects.filter(user_id = self.request.user)
# if the user is a recruiter, delete the employer field.
if Company.objects.filter(user_id = self.request.user).values('is_recruiter') == False:
pass
# self.fields.remove("employer")
del self.fields["employer"]
return context
The current error is NoneType' object has no attribute '__getitem__'.
My question: how can I remove a field from the form based on logic? I tried these versions:
self.fields["employer"].delete()
self.fields.remove("employer")
del self.fields["employer"]
Any tips?
The correct way to implement this (modify the fields of the form depending on user) is to do it on your form's __init__ method. However in order for the form to access the current user you need to pass the user to it from your view. To do this you'll use the get_form_kwargs method. Thus, start by adding the following method to your view:
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs.update({'user': self.request.user})
return kwargs
And now, you can add an __init__ to your form like this:
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
super().__init__(*args, **kwargs)
if Company.objects.filter(user_id = self.user).is_recruiter == False:
self.fields.pop("employer")
self.fields.pop('owned_by')
Notice that you first initialize the form (using super.__init__) and then you can modify the fields to your heart's content.
There are few ways to go about it.
I find having 2 separate forms RecruiterEmployeeForm and EmployeeForm may be neater.
class RecruiterEmployeeForm(forms.ModelForm):
model = Job
fields = ('title',
'job_desc',
'agency_name',
'employer',
'contact_name',
)
class EmployeeForm(forms.ModelForm):
model = Job
fields = ('title',
'job_desc',
'agency_name',
'contact_name',
)
Then you can override ger_form_class for the CBV
def get_form_class(self):
if self.request.user.is_recruiter():
return RecruiterEmployeeForm
else:
return EmployeeForm
To send extra kwargs to use generic view method get_form_kwargs and to get extra kwargs override __init__ of form and pop the extra kwargs.
forms.py
class JobCreationForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
user = kwargs.pop('user', None)
super(JobCreationForm, self).__init__(*args, **kwargs)
if Company.objects.filter(user_id = self.user).is_recruiter == False:
self.fields.pop("employer")
class Meta:
model = Job
# exclude = ['posted', 'provider', 'ext_id']
fields = ('title', 'job_desc', 'agency_name', 'employer', 'contact_name')
views.py
class JobCreateView(LoginRequiredMixin, CreateView):
template_name = 'job/job.html'
form_class = JobCreationForm
success_url = '/'
def get_form_kwargs(self):
kwargs = super(JobCreateView, self).get_form_kwargs()
kwargs.update({'user': self.request.user})
return kwargs

Dynamically exclude or include a field in Django REST framework serializer

I have a serializer in Django REST framework defined as follows:
class QuestionSerializer(serializers.Serializer):
id = serializers.CharField()
question_text = QuestionTextSerializer()
topic = TopicSerializer()
Now I have two API views that use the above serializer:
class QuestionWithTopicView(generics.RetrieveAPIView):
# I wish to include all three fields - id, question_text
# and topic in this API.
serializer_class = QuestionSerializer
class QuestionWithoutTopicView(generics.RetrieveAPIView):
# I want to exclude topic in this API.
serializer_class = ExamHistorySerializer
One solution is to write two different serializers. But there must be a easier solution to conditionally exclude a field from a given serializer.
Have you tried this technique
class QuestionSerializer(serializers.Serializer):
def __init__(self, *args, **kwargs):
remove_fields = kwargs.pop('remove_fields', None)
super(QuestionSerializer, self).__init__(*args, **kwargs)
if remove_fields:
# for multiple fields in a list
for field_name in remove_fields:
self.fields.pop(field_name)
class QuestionWithoutTopicView(generics.RetrieveAPIView):
serializer_class = QuestionSerializer(remove_fields=['field_to_remove1' 'field_to_remove2'])
If not, once try it.
Creating a new serializer is the way to go. By conditionally removing fields in a serializer you are adding extra complexity and making you code harder to quickly diagnose. You should try to avoid mixing the responsibilities of a single class.
Following basic object oriented design principles is the way to go.
QuestionWithTopicView is a QuestionWithoutTopicView but with an additional field.
class QuestionSerializer(serializers.Serializer):
id = serializers.CharField()
question_text = QuestionTextSerializer()
topic = TopicSerializer()
class TopicQuestionSerializer(QuestionSerializer):
topic = TopicSerializer()
You can set fields and exclude properties of Meta
Here is an Example:
class DynamicFieldsModelSerializer(serializers.ModelSerializer):
class Meta:
model = User
exclude = ['id', 'email', 'mobile']
def __init__(self, *args, **kwargs):
super(DynamicFieldsModelSerializer, self).__init__(*args, **kwargs)
# #note: For example based on user, we will send different fields
if self.context['request'].user == self.instance.user:
# Or set self.Meta.fields = ['first_name', 'last_name', 'email', 'mobile',]
self.Meta.exclude = ['id']
Extending above answer to a more generic one
class QuestionSerializer(serializers.Serializer):
def __init__(self, *args, **kwargs):
fields = kwargs.pop('fields', None)
super(QuestionSerializer, self).__init__(*args, **kwargs)
if fields is not None:
allowed = set(fields.split(','))
existing = set(self.fields)
for field_name in existing - allowed:
self.fields.pop(field_name)
class QuestionWithoutTopicView(generics.RetrieveAPIView):
def get_serializer(self, *args, **kwargs):
kwargs['context'] = self.get_serializer_context()
fields = self.request.GET.get('display')
serializer_class = self.get_serializer_class()
return serializer_class(fields=fields,*args, **kwargs)
def get_serializer_class(self):
return QuestionSerializer
Now we can give a query parameter called display to output any custom display format http://localhost:8000/questions?display=param1,param2
You can use to representation method and just pop values:
def to_representation(self, instance):
"""Convert `username` to lowercase."""
ret = super().to_representation(instance)
ret.pop('username') = ret['username'].lower()
return ret
you can find them here
https://www.django-rest-framework.org/api-guide/serializers/#overriding-serialization-and-deserialization-behavior

Adding a ManyToManyWidget to the reverse of a ManyToManyField in the Django Admin

Let's say I have a simple blog app in Django 1.4:
class Post(models.Model):
title = …
published_on = …
tags = models.ManyToManyField('Tag')
class Tag(models.Model):
name = …
i.e. a post has many tags. On the Django admin, I get a nice little <select multi> if I include tags in the fields for the PostAdmin. Is there an easy way to include the list of the posts (as a simple <select multi>) in the TagAdmin? I tried putting fields = ['name', 'posts'] in the TagAdmin and got an ImproperlyConfigured error. (same result for post_set).
I'm alright with Django, so could whip up a proper AdminForm and Admin object, but I'm hoping there a Right Way™ to do it.
This is possible to do with a custom form.
from django.contrib import admin
from django import forms
from models import Post, Tag
class PostAdminForm(forms.ModelForm):
tags = forms.ModelMultipleChoiceField(
Tag.objects.all(),
widget=admin.widgets.FilteredSelectMultiple('Tags', False),
required=False,
)
def __init__(self, *args, **kwargs):
super(PostAdminForm, self).__init__(*args, **kwargs)
if self.instance.pk:
self.initial['tags'] = self.instance.tags.values_list('pk', flat=True)
def save(self, *args, **kwargs):
instance = super(PostAdminForm, self).save(*args, **kwargs)
if instance.pk:
instance.tags.clear()
instance.tags.add(*self.cleaned_data['tags'])
return instance
class PostAdmin(admin.ModelAdmin):
form = PostAdminForm
admin.site.register(Post, PostAdmin)
That False in there can be replaced with a True if you want vertically stacked widget.
A bit late to the party, but this is the solution that works for me (no magic):
# admin.py
from django.contrib import admin
from models import Post
class TagPostInline(admin.TabularInline):
model = Post.tags.through
extra = 1
class PostAdmin(admin.ModelAdmin):
inlines = [TagPostInline]
admin.site.register(Post, PostAdmin)
Reference: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#working-with-many-to-many-models
Modify your models to add reverse field:
# models.py
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=100)
published_on = models.DateTimeField()
tags = models.ManyToManyField('Tag')
class Tag(models.Model):
name = models.CharField(max_length=10)
posts = models.ManyToManyField('blog.Post', through='blog.post_tags')
Then in standard way add field to ModelAdmin:
#admin.py
from django.contrib import admin
class TagAdmin(admin.ModelAdmin):
list_filter = ('posts', )
admin.site.register(Tag, TagAdmin)
Matthew's solution didn't work for me (Django 1.7) when creating a new entry, so I had to change it a bit. I hope it's useful for someone :)
class PortfolioCategoriesForm(forms.ModelForm):
items = forms.ModelMultipleChoiceField(
PortfolioItem.objects.all(),
widget=admin.widgets.FilteredSelectMultiple('Portfolio items', False),
required=False
)
def __init__(self, *args, **kwargs):
super(PortfolioCategoriesForm, self).__init__(*args, **kwargs)
if self.instance.pk:
initial_items = self.instance.items.values_list('pk', flat=True)
self.initial['items'] = initial_items
def save(self, *args, **kwargs):
kwargs['commit'] = True
return super(PortfolioCategoriesForm, self).save(*args, **kwargs)
def save_m2m(self):
self.instance.items.clear()
self.instance.items.add(*self.cleaned_data['items'])
You can add a symmetrical many to many filter this way.
Credit goes to
https://gist.github.com/Grokzen/a64321dd69339c42a184
from django.db import models
class Pizza(models.Model):
name = models.CharField(max_length=50)
toppings = models.ManyToManyField(Topping, related_name='pizzas')
class Topping(models.Model):
name = models.CharField(max_length=50)
### pizza/admin.py ###
from django import forms
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from django.contrib.admin.widgets import FilteredSelectMultiple
from .models import Pizza, Topping
class PizzaAdmin(admin.ModelAdmin):
filter_horizonal = ('toppings',)
class ToppingAdminForm(forms.ModelForm):
pizzas = forms.ModelMultipleChoiceField(
queryset=Pizza.objects.all(),
required=False,
widget=FilteredSelectMultiple(
verbose_name=_('Pizzas'),
is_stacked=False
)
)
class Meta:
model = Topping
def __init__(self, *args, **kwargs):
super(ToppingAdminForm, self).__init__(*args, **kwargs)
if self.instance and self.instance.pk:
self.fields['pizzas'].initial = self.instance.pizzas.all()
def save(self, commit=True):
topping = super(ToppingAdminForm, self).save(commit=False)
if commit:
topping.save()
if topping.pk:
topping.pizzas = self.cleaned_data['pizzas']
self.save_m2m()
return topping
class ToppingAdmin(admin.ModelAdmin):
form = ToppingAdminForm
admin.site.register(Pizza, PizzaAdmin)
admin.site.register(Topping, ToppingAdmin)

filtering dropdown values in django admin

class Foo(models.Model):
title = models.TextField()
userid = models.IntegerField()
image = models.CharField(max_length=100)
def __unicode__(self):
return self.title
class Bar(models.Model):
foo = models.ForeignKey(Foo, related_name='Foo_picks', unique=True)
added_on = models.DateTimeField(auto_now_add=True)
In Django admin add_view:
def add_view(self, *args, **kwargs):
self.exclude = ("added_on",)
self.readonly_fields = ()
return super(Bar, self).add_view(*args, **kwargs)
So, Field shows in the admin add view is foo Which is a drop down list and shows all the titles. Some title of Foo remains empty or ''. So, drop down list have lots of empty value because it title is empty. I want to filter out those empty values.
You can provide your own form for ModelAdmin, with custom queryset for foo field.
from django import forms
from django.contrib import admin
#Create custom form with specific queryset:
class CustomBarModelForm(forms.ModelForm):
class Meta:
model = Bar
fields = '__all__'
def __init__(self, *args, **kwargs):
super(CustomBarModelForm, self).__init__(*args, **kwargs)
self.fields['foo'].queryset = Foo.objects.filter(title__isnull=False)# or something else
# Use it in your modelAdmin
class BarAdmin(admin.ModelAdmin):
form = CustomBarModelForm
Something like this...
docs
for django 1.6:
For foreign key:
https://docs.djangoproject.com/en/1.6/ref/contrib/admin/#ModelAdmin.formfield_for_foreignkey
class MyModelAdmin(admin.ModelAdmin):
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "title":
kwargs["queryset"] = Foo.objects.filter(title__isnull=False)
return super(MyModelAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
I stumbled across this question when looking for a solution to filter dropdown options on the fly in the admin interface based on the selection in another field -- not based on a pre-filtered list at page load. The solution I found was this library: https://github.com/digi604/django-smart-selects which is an app that uses ajax calls and allows chain filtering to multiple levels. Works like a charm for me. -HTH
You could subclass your own model.ModelAdmin and create a custom field for your ChoiceField...
class CustomForm(model.ModelForm):
class Meta:
model = Foo
foo = forms.ChoiceField(widget=forms.Select, initial=self.foo_queryset)
def foo_queryset(self):
return Foo.objects.filter(xy)...
class FooAdmin(model.ModelAdmin):
form = CustomForm

Categories