Views don't produce migration file after makemigrations
Closed this issue · 2 comments
shawnlikescode commented
Hi! Hopefully there's just something small I'm missing. When I run makemigrations Django, don't take register the files I've created for views. My Database is Postgres.
shawnlikescode commented
from django.db import models
from dbview.models import DbView
class ModelA(models.Model):
fielda = models.CharField()
fieldc = models.IntegerField()
class MyView(DbView):
fieldA = models.OneToOneField(ModelA, primary_key=True,
db_column='fielda__id')
fieldB = models.IntegerField(blank=True, null=True, db_column='fieldb')
@classmethod
def view(cls):
"""
This method returns the SQL string that creates the view,
in this example fieldB is the result of annotating another column
"""
qs = modelA.objects.all(
).annotate(
fieldb=models.Sum('fieldc'),
).annotate(
fielda__id=models.F('pk'),
).order_by(
'fielda__id',
).values(
'fielda__id',
'fieldb',
)
return str(qs.query)
I'm testing with the basic example code.
moorchegue commented
Is this in your models.py
, or do you have a more complex models module structure? In case of the latter don't forget to have an __init__.py
inside your models/
directory and import all submodules there like:
from . import myview
__all__ = ['myview']
I did just execute this with Postgres, and there was a couple of small hiccups though. ModelA.FieldA
wanted a max_length
:
ui.ModelA.fielda: (fields.E120) CharFields must define a 'max_length' attribute.
And MyView.FieldA
was missing an on_delete
argument:
TypeError: OneToOneField.__init__() missing 1 required positional argument: 'on_delete'
I'll update the example to have those incorporated. But this doesn't seem to be your issue, right?