In Django 1.11, I have 2 models, Foo and Bar:
class Foo(models.Model):
name = models.CharField()
class Bar(models.Model):
name = models.CharField()
foo = models.ForeignKey(Foo)
When I visit the Foo page in the Django admin, I want to be able to see a list of its Bars underneath it. So I do this in admin.py:
class BarInline(admin.StackedInline):
model = Bar
#admin.register(Foo)
class FooAdmin(admin.ModelAdmin):
list_display = ('name')
fields = ('name')
inlines = [BarInline]
But what I really want is a list of clickable links to a separate page where I can edit each Bar (as well as a Add button to add a new Bar to this Foo). I.e. I don't want the entire inline form. How is this possible in Django?
admin.py
from django.urls import reverse
from django.utils.html import format_html_join
#admin.register(Foo)
class FooAdmin(admin.ModelAdmin):
list_display = ('name')
fields = ('name', get_related, )
readonly_fields = (get_related, )
def get_related(self, instance):
obj = instance.bar_set.all()
return format_html_join(
',',
'{}',
((
reverse('admin:{{ app_label }}_bar_change', args=(c.id,)),
c.name
) for c in obj),
)
You can create a callable readonly field which will return the reversed admin url of each relation wrapped in the relevant html code.
This will result in something like:
"your readonly field": link1, link2, link3
You can make all the fields readonly in BarInline.
class BarInline(admin.StackedInline):
model = Bar
readonly_fields = ('id', 'name', 'foo')
extra = 0
And for the Add functionality you can modify the inline template or more easy probably, add a custom field in FooAdmin, something like:
class FooAdmin(admin.ModelAdmin):
list_display = ('name')
fields = ('name', 'custom_add_bar')
inlines = [BarInline]
def custom_add_bar(self, obj):
add_url = reverse('admin:appname_bar_add')
return mark_safe(f'Add Bar')
Documentation for reversing admin urls
Related
I have models:
class User(models.Model):
...
group = models.ManyToManyField(
Group, related_name="users_group",
)
class Group(models.Model):
....
How to check in serializer is the Group empty (there is no relationship with User)
My version is :
class GroupSerializer(serializers.ModelSerializer):
empty = serializers.SerializerMethodField()
class Meta:
...
def get_empty(self, obj):
return not User.objects.filter(group=obj).exists()
But maybe there is an even better way.
If you define a ManyToManyModel [Django-doc], Django automatically defines one in reverse with the value for the related_name=… parameter [Django-doc] as name, so you can use:
class GroupSerializer(serializers.ModelSerializer):
empty = serializers.SerializerMethodField()
class Meta:
# …
def get_empty(self, obj):
return not group.users_group.exists()
But this is not very efficient if you want to serialize a large number of Groups: for each Group it will make an extra query. You can use a BooleanField:
class GroupSerializer(serializers.ModelSerializer):
empty = serializers.BooleanField()
class Meta:
# …
and then in the APIView or the ViewSet use as QuerySet a QuerySet where you make an annotation with an Exists subquery [Django-doc] to check if there exists a user for that Group:
from django.db.models import Exists, OuterRef
from rest_framework import viewsets
class MyModelViewSet(viewsets.ModelViewSet):
queryset = Group.objects.annotate(
empty=~Exists(
User.objects.filter(group=OuterRef('pk'))
)
)
# …
Suppose I have a model of Category, and it has been declared in admin.py.
There are two things that I want to do using Django template overriding.
Add a button on the right near "Add Category +" which is visible only on the Category List page and takes me to another URL.
Overriding URLs of Category object so that clicking on each individual category on the list takes to the respective URLs
# models.py
class Category(models.Model):
name = models.CharField(max_length=50, null=True, blank=False)
LANGUAGE_ENGLISH = 'en'
LANGUAGE_FRENCH = 'fr'
LANGUAGES = ((LANGUAGE_ENGLISH, 'English'),(LANGUAGE_FRENCH, 'French'),)
language = models.CharField(max_length=12, default=LANGUAGE_ENGLISH, choices=LANGUAGES, blank=False)
created_at = models.DateTimeField(auto_now_add=True)
# admin.py
#admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
list_display = ('name', 'language', 'created_at')
list_filter = ('created_at', 'language')
search_fields = ('name',)
date_hierarchy = 'created_at'
ordering = ['-created_at']
Category in the admin panel
Here, clicking on Lifestyle or Travel should take me to two external urls.
First solution:
override list_display_links and change your field manually
It's a two steps process.
First, we need to change get_list_display_links default behavior.
Looking at django's doc and source you will realize it will end up using first item in list_display.
In your admin class:
#admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
list_display = ('name', 'language', 'created_at')
list_filter = ('created_at', 'language')
list_display_links = [] #< With this, you still can add up a link to your original admin
search_fields = ('name',)
date_hierarchy = 'created_at'
ordering = ['-created_at']
def get_list_display_links(self, request, list_display):
"""
Return a sequence containing the fields to be displayed as links
on the changelist. The list_display parameter is the list of fields
returned by get_list_display().
"""
if self.list_display_links or self.list_display_links is None or not list_display:
# We make sure you still add your admin's links if you explicitly declare `list_display_links`
return self.list_display_links
else:
# We return empty list instead of `list_display[:1]`
# if no `list_display_links` is provided.
return []
Then using this answer, you can make any column customized.
Second solution:
Handle change view yourself
In your admin class:
#admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
#... same things as you have
def change_view(self, request, object_id, form_url="", extra_context=None):
#Now, you can do pretty much whatever: it's a function based view!
I'd recommend the first one as I believe default admin's change_view is always usefull.
In Django 1.11, I have 2 models, Foo and Bar:
class Foo(models.Model):
name = models.CharField()
extra = models.BooleanField(default=False)
class Bar(models.Model):
name = models.CharField()
extra_foo = models.ForeignKey(Foo)
My admin.py looks like this:
class BarInline(admin.StackedInline):
model = Bar
fields = ('name', 'extra_foo')
class FooAdmin(admin.ModelAdmin):
fields('name')
inlines = [BarInline]
My problem is that the in the Bar inline form, the dropdown for extra_foo shows all of my existing Foos. I want it to only show Foos for which extra is true. How can I modify the admin to restrict the available options in a select box to a subset of the whole?
I guess, you can use render_change_form
class FooAdmin(admin.ModelAdmin):
def render_change_form(self, request, context, *args, **kwargs):
context['adminform'].form.fields['extra_foo'].queryset = Foo.objects.filter(extra=True)
return super(FooAdmin, self).render_change_form(request, context, *args, **kwargs)
admin.site.register(Foo, FooAdmin)
If you want this to be global then have a look at limit_choices_to
extra_foo = models.ForeignKey(Foo, limit_choices_to={'extra': True})
Credit : filter foreignkey field in django admin
You can achieve that by using limit_choices_to. Change this line:
extra_foo = models.ForeignKey(Foo)
to:
extra_foo = models.ForeignKey(Foo, limit_choices_to={'extra': True})
This will limit the choices for extra_foo to Foo objects that have the property extra set to True.
This approach will however limit the choices not only in the admin but also beyond. If you need to adjust only the admin, you need a different approach.
I'm creating a web application with Django.
In my models.py I have a class BaseProduct and a class DetailProduct, which extends BaseProduct.
In my admin.py I have BaseProductAdmin class and DetailProductAdmin class, which extends BaseProductAdmin.
I have another class called System, with a many to many relation with BaseProduct.
In the System admin page, I can visualize a list of the BaseProduct objects related to that system.
When I click on a product, the application redirect me to the BaseProduct admin page.
When a product of the list is a DetailProduct object, I would like to be redirected on the DetailProduct admin page instead.
Any idea on how to do this?
In models.py :
class BaseProduct(models.Model):
id = models.AutoField(primary_key=True, db_column='ID')
_prod_type_id = models.ForeignKey(
ProductTypes, verbose_name="product type", db_column='_prod_type_ID')
systems = models.ManyToManyField(
'database.System', through='database.SystemProduct')
def connected_to_system(self):
return self.systems.exists()
class Meta:
db_table = u'products'
verbose_name = "Product"
ordering = ['id', ]
class System(models.Model):
id = models.AutoField(primary_key=True, db_column='ID')
name = models.CharField(max_length=300)
def has_related_products(self):
""" Returns True if the system is connected with products. """
return self.products_set.exists()
class Meta:
managed = False
db_table = u'systems'
verbose_name = "System"
ordering = ['id', ]
class DetailProduct(BaseProduct):
options_id = models.AutoField(db_column='ID', primary_key=True)
product = models.OneToOneField(BaseProduct, db_column='_product_ID', parent_link=True)
min_height = models.FloatField(help_text="Minimum height in meters.")
max_height = models.FloatField(help_text="Maximum height in meters.")
def __init__(self, *args, **kwargs):
super(DetailProduct, self).__init__(*args, **kwargs)
if not self.pk:
self._prod_type_id = ProductTypes.objects.get(pk=9)
class Meta:
managed = False
db_table = 'detail_product'
verbose_name = "Detail product"
verbose_name_plural = "Detail products"
class SystemProduct(models.Model):
id = models.AutoField(primary_key=True, db_column='ID')
_system_id = models.ForeignKey(System, db_column='_system_ID')
_product_id = models.ForeignKey(BaseProduct, db_column='_Product_ID')
class Meta:
db_table = u'system_product'
unique_together = ('_system_id', '_product_id')
verbose_name = "system/product connection"
In my admin.py page:
class SystemProductInlineGeneric(admin.TabularInline):
model = SystemProduct
extra = 0
show_edit_link = True
show_url = True
class SystemProductForm(forms.ModelForm):
class Meta:
model = SystemProduct
fields = '__all__'
def __init__(self, *args, **kwargs):
""" Remove the blank option for the inlines. If the user wants to remove
the inline should use the proper delete button. In this way we can
safely check for orphan entries. """
super(SystemProductForm, self).__init__(*args, **kwargs)
modelchoicefields = [field for field_name, field in self.fields.iteritems() if
isinstance(field, forms.ModelChoiceField)]
for field in modelchoicefields:
field.empty_label = None
class SystemProductInlineForSystem(SystemProductInlineGeneric):
""" Custom inline, used under the System change page. Prevents all product-system
connections to be deleted from a product. """
form = SystemProductForm
raw_id_fields = ("_product_id",)
class SystemAdmin(admin.ModelAdmin):
inlines = [SystemProductInlineForSystem]
actions = None
list_display = ('id', 'name')
fieldsets = [('System information',
{'fields': (('id', 'name',), ),}),
]
list_display_links = ('id', 'configuration',)
readonly_fields = ('id',)
save_as = True
If I understand correctly, your question is how to change the InlineAdmin (SystemProductInlineForSystem) template so the "change link" redirects to the DetailProduct admin change form (instead of the BaseProduct admin change form) when the product is actually a DetailProduct.
I never had to deal with this use case so I can't provide a full-blown definitive answer, but basically you will have to override the inlineadmin template for SystemProductInlineForSystem and change the part of the code that generates this url.
I can't tell you exactly which change you will have to make (well, I probably could if I had a couple hours to spend on this but that's not the case so...), so you will have to analyze this part of the code and find out by yourself - unless of course someone more knowledgeable chimes in...
class Product(models.Model):
products = models.CharField(max_length=256)
def __unicode__(self):
return self.products
class PurchaseOrder(models.Model):
product = models.ManyToManyField('Product')
vendor = models.ForeignKey('VendorProfile')
dollar_amount = models.FloatField(verbose_name='Price')
I have that code. Unfortunately, the error comes in admin.py with the ManyToManyField
class PurchaseOrderAdmin(admin.ModelAdmin):
fields = ['product', 'dollar_amount']
list_display = ('product', 'vendor')
The error says:
'PurchaseOrderAdmin.list_display[0]', 'product' is a ManyToManyField
which is not supported.
However, it compiles when I take 'product' out of list_display. So how can I display 'product' in list_display without giving it errors?
edit: Maybe a better question would be how do you display a ManyToManyField in list_display?
You may not be able to do it directly. From the documentation of list_display
ManyToManyField fields aren’t supported, because that would entail
executing a separate SQL statement for each row in the table. If you
want to do this nonetheless, give your model a custom method, and add
that method’s name to list_display. (See below for more on custom
methods in list_display.)
You can do something like this:
class PurchaseOrderAdmin(admin.ModelAdmin):
fields = ['product', 'dollar_amount']
list_display = ('get_products', 'vendor')
def get_products(self, obj):
return "\n".join([p.products for p in obj.product.all()])
OR define a model method, and use that
class PurchaseOrder(models.Model):
product = models.ManyToManyField('Product')
vendor = models.ForeignKey('VendorProfile')
dollar_amount = models.FloatField(verbose_name='Price')
def get_products(self):
return "\n".join([p.products for p in self.product.all()])
and in the admin list_display
list_display = ('get_products', 'vendor')
This way you can do it, kindly checkout the following snippet:
class Categories(models.Model):
""" Base category model class """
title = models.CharField(max_length=100)
description = models.TextField()
parent = models.ManyToManyField('self', default=None, blank=True)
when = models.DateTimeField('date created', auto_now_add=True)
def get_parents(self):
return ",".join([str(p) for p in self.parent.all()])
def __unicode__(self):
return "{0}".format(self.title)
And in your admin.py module call method as follows:
class categories(admin.ModelAdmin):
list_display = ('title', 'get_parents', 'when')
If you want to save extra queries, you can use prefetch_related in the get_queryset method like below:
class PurchaseOrderAdmin(admin.ModelAdmin):
fields = ['product', 'dollar_amount']
list_display = ('get_products', 'vendor')
def get_queryset(self, request):
qs = super().get_queryset(request)
return qs.prefetch_related('product')
def get_products(self, obj):
return ",".join([p.products for p in obj.product.all()])
According to the Docs, In this way, there would be just one extra query needed to fetch related Product items of all PurchaseOrder instances instead of needing one query per each PurchaseOrder instance.