Skip to content

Django raw ID fields – explained (basically)

Old content alert!

Thanks very much for visiting my blog. Always happy to have people read my stuff but just thought I should point out this post is years old. If it's about technical stuff there is a possibility that it is no longer relevant, if it's a bug report/fix I'm hoping that it should now have been fixed.

Of course if this is what you're looking for and it does work then great, just don't say I didn't warn you...

Raw ID fields seem to be a really cool django feature that allows you to link one model into another and provide a pretty nice UI tool that allows your users (editorial) to select which model to include. Unfortunately a quick Google seems to return API docs (which for thicko’s like me are fairly difficult to understand).

Thankfully after using them in my day job I’m happy to say that they are actually a piece of cake to do – and I’m going to explain how (at least how I did them).

Django makes it really easy to insert a foreign key relationship between models. All you need do is use the native ForeignKey field type into your models.py. For example:


#you need to import the model you want to ForeignKey into of course
from django.contrib.sites.models import RossLikes

class Ross(models.Model):
    name = models.CharField(max_length-512)
    description = models.TextField()
    image = models.ImageField(upload_to='images')
    likes = models.ForeignKey(RossLikes)

This will provide you with an interface like below.

Django admin with the standard foreign key display (a big select box with ALL possible values)

It’s got all you need in there and can select any individual record in the RossLikes model to include alongside Ross. Thinking that you have a large number of things to pick from this kind of interface will quickly become a PITA. This is where raw ID fields come into play!

All you need to do it include this one line into your models modelAdmin method in admin.py:


class RossAdmin(admin.ModelAdmin):
    raw_id_fields = ('likes')

You’ll now get a nice picker style widget that provides you with all the values you had before in the select box with the extra win of being able to filter, search (whatever options you provide for the linked in model when you access it on it’s own!)

When raw ID's are set up you get a picker link which opens up a filtered view of the linked model. Pick anything you like (and find it easier)

I find this very helpful and I reckon you will to!

  • http://queryclick.com Life Design SEO

    Hi Ross,

    Got here searching for solutions to dynamically calling Django’s ID for a custom feec type. Can’t see anything here to help with that but it’s a handy post anyway!

  • http://magicrebirth.wordpress.com/ mik

    is there any way to get the link generated by the raw_id option pull out a full-featured edit/select window? (now it just lets you select an object, but what if you wanted to edit it before selecting it? )

  • http://www.webtrafficroi.com ZK@Web Marketing Blog

    Raw ID fields seem to be a really cool django feature that allows you to link one model into another and provide a pretty nice UI tool that allows your users (editorial) to select which model to include. Unfortunately a quick Google seems to return API docs (which for thicko’s like me are fairly difficult to understand).

  • Tomasz Zieli?ski

    Just one note, this:

    raw_id_fields = (‘likes’)

    doesn’t assign a tuple to raw_id_fields, you would need to use (‘likes’,) /note the trailing comma/.

  • Pingback: Ajax foreign key filter in Django admin interface « Skylab Blog