开发者

Pre-Filling a ForeignKey from a link to the Admin?

开发者 https://www.devze.com 2022-12-15 19:10 出处:网络
Some introduction. I have a \"planet-like\" feed aggregator with an extra layer on top. That extra layer allows for comments and elaboration on the aggregated posts. Here\'s some code for reference.

Some introduction. I have a "planet-like" feed aggregator with an extra layer on top. That extra layer allows for comments and elaboration on the aggregated posts. Here's some code for reference.

class Post(models.Model):
    title = models.CharField()
    published = models.DateTimeField()

class Story(models.Model):
    title = models.CharField()
    post = models.ForiegnKey(Post)

Story has a ForeignKey to Post and when I write a story I pick a 开发者_JAVA百科post from the drop-down. Now, after a few weeks the list could get pretty unruly. I could use raw_id_fields, but that's a bit counter-intuitive since I would have to find the ID of the post I needed.

EDIT: After doing some research, I removed my misleading question. I'm wondering if something like this is possible (given that application is the name of my... application.

<a href="/admin/application/story/add/?post=[post.id]">Write about this post.</a>

Let me know if THIS needs any more explanation. :)


Looks like the admin recognizes GET values. So,

/admin/application/story/add/?post=[post.id]

would set post to the proper ID. :)


You might want to think about using an autocomplete field instead of raw_id_fields.

Jannis Leidel has a good explanation, with examples, on how to add auto-complete functionality to the djando admin exactly for cases like yours.

You will need to add jquery to the mix, but the process is not that complicated.

0

精彩评论

暂无评论...
验证码 换一张
取 消