python - Django admin - TabularInline - display only a few objects from other model -


example models:

class book(models.model):     types = (         (0, 'sci-fi')         (1, 'biography')     )      title = models.charfield(...)     book_type = models.charfield(max_length=1, choices=types)   class connect(models.model):      book1 = models.foreignkey(book, related_name='book_1')      book2 = models.foreignkey(book, related_name='book_2') 

i want 1 think, display connect tabularinline in book, show connect book1__book_type = 0

i try this:

class connectformset(baseinlineformset):     def get_queryset(self):         if not hasattr(self, '_queryset'):             qs = super(connectionformset, self).get_queryset().filter('book1__book_type':0)             self._queryset = qs         return self._queryset    class inlineconnectn(admin.tabularinline):     fk_name = 'book1'     model = connect     = 0     formset = connectformset 

but it's not working want. still connections visible in list of connect in tabularinline.

works save connect (only connect book1__book_type = 0 saved).

formfield_for_foreignkey should want:

class inlineconnectn(admin.tabularinline):     def formfield_for_foreignkey(self, db_field, request, **kwargs):         if db_field.name == "book1":             kwargs["queryset"] = foo.objects.filter(book1__book_type=1)         return super(inlineconnectn, self).formfield_for_foreignkey(db_field, request, **kwargs) 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -