开发者

Django multiple many to many fields?

开发者 https://www.devze.com 2022-12-25 18:41 出处:网络
I am building a news app for my website. I want to use a sort of tag system. Each news article can have different and 开发者_JS百科multiple tags. All tags are saved in a tag model, and i want to conne

I am building a news app for my website. I want to use a sort of tag system. Each news article can have different and 开发者_JS百科multiple tags. All tags are saved in a tag model, and i want to connect the tags to the newsarticle. Now is this possible with: tags = models.ForeignKey( TagsModel ) for one tag, but how i can do this with multiple of them?

Thank you!


Use django tagging.

In your model you do: tags = TagField() and presto, you have tags that behave like you expect. The app also comes with several niceties to perform common tasks. e.g. parse input into tags or output the tags in templates.

In general, though, you can have ManyToMany fields like so:

some_things = models.ManyToManyField(OtherModel)

here are the docs for that. You can have multiple ManyToManyFields in a single model, you just need to specify related names.

0

精彩评论

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

关注公众号