django admin how to limit selectbox values
Posted
by SledgehammerPL
on Stack Overflow
See other posts from Stack Overflow
or by SledgehammerPL
Published on 2010-06-11T10:09:11Z
Indexed on
2010/06/11
10:12 UTC
Read the original article
Hit count: 348
model:
class Store(models.Model):
name = models.CharField(max_length = 20)
class Admin:
pass
def __unicode__(self):
return self.name
class Stock(Store):
products = models.ManyToManyField(Product)
class Admin:
pass
def __unicode__(self):
return self.name
class Product(models.Model):
name = models.CharField(max_length = 128, unique = True)
parent = models.ForeignKey('self', null = True, blank = True, related_name='children')
(...)
def __unicode__(self):
return self.name
mptt.register(Product, order_insertion_by = ['name'])
admin.py:
from bar.drinkstore.models import Store, Stock
from django.contrib import admin
admin.site.register(Store)
admin.site.register(Stock)
Now when I look at admin site I can select any product from the list. But I'd like to have a limited choice - only leaves. In mptt class there's function:
is_leaf_node() -- returns True if the model instance is a leaf node (it has no children), False otherwise.
But I have no idea how to connect it
I'm trying to make a subclass: in admin.py:
from bar.drinkstore.models import Store, Stock
from django.contrib import admin
admin.site.register(Store)
class StockAdmin(admin.ModelAdmin):
def queryset(self, request):
qs = super(StockAdmin, self).queryset(request).filter(ihavenoideawhatfilter)
admin.site.register(Stock, StockAdmin)
but I'm not sure if it's right way, and what filter set.
© Stack Overflow or respective owner