Purgeable / full-app

Keep all functionality for (parsers + db + frontend API) inside one Django project.
0 stars 1 forks source link

datamodel: change Datapoint structure #4

Closed epogrebnyak closed 7 years ago

epogrebnyak commented 7 years ago
epogrebnyak commented 7 years ago

Here I'll have minor changes, this can be fixed easily

epogrebnyak commented 7 years ago

@tinivir , need some help for proper setup here. I want to edit https://github.com/mini-kep/full-app/blob/master/datapoint/models.py + run it in python to see if it works.

Following this I run:

python manage.py shell

in ipyton I get:


In [8]: run models.py
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
~\Desktop\mini-kep\full-app\datapoint\models.py in <module>()
      5
      6
----> 7 class Datapoint(models.Model):
      8     """
      9     Create table 'Datapoint' in Db with fields:

...\Anaconda3\lib\site-packages\django\db\models\base.py in __new__(cls, name, bases, attrs)
    116                         "Model class %s.%s doesn't declare an explicit "
    117                         "app_label and isn't in an application in "
--> 118                         "INSTALLED_APPS." % (module, name)
    119                     )
    120

RuntimeError: Model class __main__.Datapoint doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

What is the source of error?

tinivir commented 7 years ago

You can't run directly this file models.py from python manage.py shell. If you change models you need to follow next steps:

  1. python manage.py makemigrations datapoint
  2. python manage.py migrate
  3. python manage.py runserver
  4. go to 127.0.0.1:8000/admin to see your changes
epogrebnyak commented 7 years ago

Не очень понятно, как редактировать models.py - вслепую?

epogrebnyak commented 7 years ago

Локально никак не запустить?

epogrebnyak commented 7 years ago

нужно сделать миграции: создать и применить, а потом запустить сервер и посмотреть уже в админке что и как мы же меняем бд меняя models. но нужно об этом сообщить python, то есть сделать миграции

epogrebnyak commented 7 years ago

I think I was in need of something like this: http://django.readthedocs.io/en/latest/releases/1.7.html#standalone-scripts

epogrebnyak commented 7 years ago

My takeaway: Django ORM not meant for standalone usage, but if one really wants below is a way to go.


# boilerplace to standalone code

import django
from django.conf import settings
if not settings.configured:
    settings.configure(DEBUG=True, LOGGING_CONFIG=None)
django.setup()

# standalone code

from django.db import models
class Datapoint(models.Model):
    """
    Create table 'Datapoint' with fields:
      - freq
      - name
      - date
      - value
    """
    ALLOWED_FREQUENCIES = (
        ('a', 'annual'),
        ('q', 'quarterly'),
        ('m', 'monthly'),
        ('w', 'weekly'),
        ('d', 'daily')
    )

    freq = models.CharField(max_length=1, choices=ALLOWED_FREQUENCIES)
    name = models.CharField(max_length=64, null=False)
    date = models.DateField(null=False)
    value = models.FloatField(null=False)

    class Meta:
        unique_together = ('freq', 'name', 'date')
        # Supress following code:            
        # RuntimeError: Model class __main__.Datapoint doesn't declare an explicit 
        # app_label and isn't in an application in INSTALLED_APPS. 
        app_label = 'my_app'

#> 325: RuntimeWarning: Model 'my_app.datapoint' was already registered. 
#> Reloading models is not advised as it can lead to inconsistencies, 
#> most notably with related models.
epogrebnyak commented 7 years ago

@tinivir - can you please review this - a00c6b2403ea3bce0f0d7042d6f82546d5a43cc4

Can close this issue after review.