How to get the reverse of a many to many relationship in Django? - django

I have a model for articles with various attributes like author, title, etc. One of the attributes is "comments_on" for all of the articles that the article in question (let's call it "main article" for now) is responding to.
I use a reciprocal many-to-many relationship to represent this.
I know how to display on a website all of the articles that the main article comments on. I would like to know if it's possible to display all of the articles that have commented on the main article.
I assume I don't need to create a separate field in my model for this, since the relationship between the main article and other articles that comment on the main article is reflected in the same reciprocal many-to-many relationship.
Any idea what the code for this is will be? Thank you.

I think you're looking for related_name.

Related

Django Many to Many - polymorphic

In a Django application, I have three models:
Service
Product
AppliedTax
I want to create a many-to-many relationship between Service and AppliedTax and between Product and AppliedTax. Normally I would have an intermediate model (i.e. AppliedTaxTaxable) with three fields (applied_tax_id, taxable_type and taxable_id) and taxable_type + taxable_id would be a composite foreign key for each related model with taxable_type having as value the name of the related model (Service or Product in my case) and the taxable_id field the id of the related record of the respective model.
I understand how to make it work with raw queries, but Is there a "Django" way to do that with a ManyToMany relationship? So far I had no luck with that. I don't have much experience with Django, but I hope there is a way to do that without using raw queries.
Help :)
Well, after some thought, I did a better search and stumbled upon django-polymorphic. Here is a pretty straightforward explanation on how it works, for a basic set up and it does what I am describing in my question. The implemented schema differs a bit from what my description, but in the end we will home one intermediate table for all associated models.

Design of models for better perfomance

My main task is to implement likes and comments in django application, but I have concerns regarding model structure and overall architecture of application.
What I want to achieve:
Basically I have only two models (for example Book and Author), which I want to be liked, shared and commented. So obviously, I need to create corresponding tables. The question is, how will be better to reference each Like, Share and Comment in Book and Author rows.
1-st solution: The first thing which came in mind is just to add corresponding ForeignKeys in Like, Share and Comment which will point to Author and Book. So, for example Like table will be in next form:
|---------|-----------|-----------|
| ID | AUTHOR_ID | BOOK_ID |
Where ID is id of Like, AUTHOR_ID and BOOK_ID are ForeignKeys to Author and Book rows.
The problem of this solution is that if you want to add ability to 'like' more stuff you will need to add new columns to Like table. I think this solution is hacky, since Like table can grow up very quickly.
2-nd solution: I have read this question where solution suggested to create a parent table, which can be liked and then inherit from it Book and Author tables.
This solution seems very nice to me, but now concern is about concrete inheritance in Django ORM. In book Two scoops of Django authors recommend to avoid it almost everywhere.
Could you please help me with advice, whether or not should I choose Multitable (Concrete) Inheritance in order to achieve what I want? Or maybe another, more beatiful and clean solution?
Thanks
This is exactly the sort of problem that generic relations were created to solve. The documentation on that link describes a tagging system, but it can just as easily be applied to likes.

Django save model and its children with ModelForm

I have such models: Article, Link & ArticleLink. Article can have many links attached to it and I need to save them from one form.
I have created all the relations and a ModelForm for Article, but the trick part is that links have two fields: Name and URL. I can't figure out what kind of form structure must be used, I tried hidden inputs with name "links[name]" and "links[url]" but did not work.
Where should I look? Is there any working example for this? Django docs does not help with this particular situation.
You should take a look at inline formsets.

Django | Advice for making models that are related with out model inheritances for a blog post type

I have been reading about model inheritances in django and have found that many are not for it, though I'm not 100% sure why.
So I have a question for all of you who think model inheritances is bad.
I have a case were I have a blog with many sub types of a post model.
for an example lets say post1 is a common post type and post2 is a audio post type. Both post types have a title and date and other common fields, but also have unique fields used just for for their respective use.
First Q: what is the best way to make the models for both Post1 and Post2?
Second Q: what is the best way to get a list with all post types in order of date for showing in the blog roll?
--:Edit:-- I don't want this to be a debate if model / db inheritances is good or bad, I just want to find a contrast to what I think can be done. I have seen the debate many times over in the oop world about inheritance vs composition design, I know that I could use inheritance in django but I want to learn how to do it the as a composition design pattern.
Here is a good read for this issue that outlines some of the problems I have found with db inheritance http://ankhos.com/2010/01/15/django-polymorphism-mixins-vs-inheritance-models/
1Q: You need something like polymorphism to save your two types of posts in one blog. I really liked this approach:
Manager-based polymorphic model inheritance
2Q: It's good explained in the doc string.
EDIT: read your edit. It seems like you want to try an other approach.

Different models sharing one ID list

In my application I've added a "Facebook Comment Box" (on different pages, for different objects). Each object has its own comments list so I need to provide a unique (across the site) ID for every single one of them.
What would be the best approach for achieving this: An abstract model, from which all other models will inherit? A dummy model with a ForeignKey relation? What are your ideas?
You may want to implement GUIDs:
http://www.codecommit.com/blog/database/are-guids-really-the-way-to-go
Here's a django module that gives you a field for 'em:
http://pypi.python.org/pypi/softwarefabrica.django.utils/
... you can safely use them in URLs -- won't be pretty, but for comments and other things without obvious URL-able titles, GUIDs work out well.
Solved with a dummy model :
http://fromzerotocodehero.blogspot.com/2010/11/providing-uniqueness-of-different.html