1

I am trying to create a data model in Django, and would like some help from those familiar with Django ORM to advice the right approach to create the data models.

The idea: I want to store a list of hashtags that are associated in social media posts. 1 post can have multiple hashtags.

Question 1: Is below the right data representation of the model above?

Hashtag table:
==============
 - hashtag_id (primary key)
 - hashtag_name

Post table:
============
 - Post_id (primary key)
 - Post_name

Hashtag-post link table:
=========================
 - hashtag_id (link to id in hashtag table, need to specify primary key?)
 - Post_id (link to id in post table)

Question 2: How do I create the ORM model with proper primary/foreign keys?

Question 3: How do I make this query: Query all the posts associated in a hashtag in Django ORM?

2
  • Every hashtag should have it's own post, associated with it. It would be better to use Foreign Key in Post -> Hashtag Commented Sep 23, 2015 at 7:23
  • Please refer to this very useful page on Django's official website Commented Sep 23, 2015 at 7:28

1 Answer 1

3
  • Question 1: Is below the right data representation of the model above?

Yes, but you do not need to define the many-to-many table yourself. Django does it for you.

You need to do it in case you want to add attributes to the relationship, in which case you define a 'through-table'. Please read here for more info on that.

  • Question 2: How do I create the ORM model with proper primary/foreign keys?

You models could be of this sort:

from django.db import models
class Hashtag(models.Model):
    name = models.CharField(max_length=80)

    def __str__(self):              # __unicode__ on Python 2
        return self.name

class Post(models.Model):
    name = models.CharField(max_length=80)
    hashtags = models.ManyToManyField(Hashtag)

    def __str__(self):              # __unicode__ on Python 2
        return self.name

Again: you do not need to define the models' primary keys, Django does it for you (you still can if you need to).

Django adds an id field to the model automatically and that is its default primary key (also known as pk).

  • Question 3: How do I make this query: Query all the posts associated in a hashtag in Django ORM?

In several ways, e.g.:

Post.objects.filter(hastags__pk=1)
Post.objects.filter(hastags=1)
Post.objects.filter(hashtags=h1)  # where h1 is an hashtag object instance
Post.objects.filter(hashtags__name__startswith="Science")

Please refer to this very informative and useful page on Django's website.

And this one about making queries that span relationships.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.