Design of models for better perfomance - django

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.

Related

How to get the reverse of a many to many relationship in 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.

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.

Django end-user defined fields, how to? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Django dynamic model fields
Good Morning guys!
Scenario is the following. For some models on Django, I would like to allow the end user to define his own fields. It would be great if I could keep all Django awesome features like the ORM, so I can still do calls like field__gte to search on the model, still have field validation according to field type, etc. I've thought about two ways of doing this, and I'm more than open for new suggestions. Any feedback would be VERY appreciated.
The first approach, is the Entity-Attribute-Value ( http://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model ), which django already has an app for. See http://code.google.com/p/django-custom-field/
I think this would be an OK solution, but I lose the ability to do "mymodel.objects.filter(custom_field_x=something)". Maybe there's a way to regain the ORM, any ideas? But I've heard so many bad stories about this method that I'm little scared to use it.
The second approach would be to have a database table for each of the users (probably no more than a 1000). I've read django has something in the lines of inspectdb, which actually checks which fields are there and produces the model for you. This could be useful but I think maybe I should store the fields this particular user has created and somehow dinamically tell django, hey, we also have this fields in this model. Is this possible? I know it's generally bad to have different tables for each user, but considering this scenario, how would you guys rate this method, would it be ok to have one table for each user?
The model that requires custom fields is for example Person. They might want a custom field to store address, blood type, or any other thing.
MANY THANKS in advance! Have a nice sunday!
Very similar: How to create user defined fields in Django -- but only talks about the EAV, which I would like to avoid. I'm open for new ideas!
One approach is to use a NoSQL document-based solution such as MongoDB which allows you to store objects that have a fluid structure (no such restrictions as pre-defined columns).
Pros:
No restriction on custom field types, number of types of fields, etc.
Retains ORM functionality (django-mongodb)
Other various benefits of NoSQL - which you can read about online
Avoids EAV
Cons:
Need to setup NoSQL server
Additional knowledge required on NoSQL concepts (documents vs. tables)
You may have to maintain two databases - if you decide not to migrate your entire solution to NoSQL (multi-db)
EDIT:
After reading the comments its worth pointing out that depending on which NoSQL solution you go with, you may not need reversion support. CouchDB, for example has built in support for document versioning.
what about creating another model for storing user_defined_fields?
class UserDefinedField(models.Model):
#..................
user = models.ForeignKey(User)
field_name = models.CharField(max_length=50)
field_value = models.TextField()
Then you can do UserDefinedField.objects.filter(field_name=some_name,field_value=somevalue)

Filtering a Class and Subclass in Django

I have a project with an FAQ app. The app has models for FAQ (written by the site authors) and UserFAQ (written by users-- not just a clever name). I want to return all entries, FAQ or UserFAQ that match certain conditions, but I also want to exclude any UserFAQs that don't match a certain criteria. Ideally, it would looks something like:
faqs = FAQ.objects.filter(question__icontains=search).exclude(show_on_site=False)
Where "show_on_site" is a property that only UserFAQ objects have. That doesn't work because the filter craps out on the parent class as it doesn't posses the property. What's the best way of doing this? I came across this snippet, but it seems like overkill for what I want to do.
In your position, absent a need to have two tables, I'd be tempted to have one FAQ model/table with is_user_faq and show_on_site fields.
Sometimes it helps when modeling data to organize it for simple and fast access. While model inheritance has some appeal, I've found it it's often easier to avoid using it.

about annotate django

I'd like to create the top five best seller of the products in each month.
I've heard that the annotations must be used for this case but I don't know how to use it.
Will anybody be kind enough to help me ??
You need to provide much more information before anyone can provide you with a useful answer. Describe what you know, what you've tried, elaborate on what you hope to accomplish.
In the mean time, you can learn about django's annotate() function, and see a few examples by reading the aggregation documentation for django.
annotate is not the only way to do it. You could also use an extra aggregation, though it might not be very efficient, and this will rely on sql specific to your database. This approach will not work in App Engine, but should in a SQL compliant database.
For example, say you had a Product model and a SalesOrder model, where the SalesOrder model has a line item for each product_id and order_date in it.
top_products = Product.objects.extra(
select={'num_orders':'select count(product_id) from app_product where app_salesorder.order_date > thirty_days_ago and app_product.id = app_salesorder.product_id'})
.order_by('-num_orders')[:5]