Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin raw_id_fields table display

Django raw_id_fields don't display tables the way I expect, am I doing something wrong? I'm trying to use a raw_id_fields widget to edit a ForeignKey field as follows:

#models.py
class OrderLine(models.Model):
    product = ForeignKey('SternProduct')

class SternProduct(models.Model):
    def __unicode__(self): return self.product_num
    product_num = models.CharField(max_length=255)

#admin.py
#import and register stuff  
class OrderLineAdmin(admin.ModelAdmin):  
    raw_id_fields=('product')  

I get the little textbox and magnifier widget as expected, but clicking the magnifier gives me this: flickr.com/photos/28928816@N00/5244376512/sizes/o/in/photostream/
(sorry, can't post more than one hyperlink apparently)

I thought I would get something closer to the changelist page c/w columns, filters and search fields. In fact, that's apparently what others get.

Any thoughts about how to enable the more featureful widget?

like image 529
Brendan Avatar asked Jun 24 '26 19:06

Brendan


1 Answers

Ah, OK, this should have been obvious, but it isn't explained in the Django docs. The list that appears in the raw_id_fields popup uses the same options as the admin object for the referenced model. So in my example, to get a nice looking popup I needed to create a SternProductAdmin object as follows:

class SternProductAdmin(admin.ModelAdmin):
    list_display = ('__unicode__', 'drawing_number', 'drawing_revision',)
    list_filter = ('drawing_number',)
    search_fields = ('drawing_number',)
    actions = None

Hopefully this will help others in the future.

like image 116
Brendan Avatar answered Jun 27 '26 12:06

Brendan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!