0

I am trying to create materialized view in postgresql using django by following this blog post.

my models are:

class Occasion(models.Model): 
    name = models.CharField(max_length=100)

class ItemType(models.Model):
    name = models.CharField(max_length=100)

class Dress(models.Model):
    name = models.CharField(max_length=100)
    item_type = models.ForeignKey(ItemType)
    occasions = models.ManyToManyField(Occasion)
    # ... more fields

Now, I create a model corresponding to the materialized view (purpose of creating this view is to minimize join queries):

class DressMaterializedView(models.Model):
    name = models.CharField(max_length=100)
    item_type_name = models.CharField(max_length=100)  # note this
    occasion_names = models.ArrayField(  # and this
        models.CharField(max_length=100)  # and compare it with Dress model above
    )
    
    class Meta:
        managed = False
        db_table = "dresses_dressmv"

Now, What SQL query (ie. CREATE MATERIALIZED VIEW dresses_dressmv ...) do I write to create the materialized view I intend to.

Thanks in advance.

Community
  • 1
  • 1
Amrullah Zunzunia
  • 1,617
  • 16
  • 31

1 Answers1

0

Why dont you use Arrayfield provided by django OR for Django-Postgres you can have Jsonfield option but for that you need to use django 1.9 and postgres 9.4 mandatory

for reference you can have look at This

Piyush S. Wanare
  • 4,703
  • 6
  • 37
  • 54