Closed epogrebnyak closed 7 years ago
Here I'll have minor changes, this can be fixed easily
@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?
You can't run directly this file models.py from python manage.py shell. If you change models you need to follow next steps:
Не очень понятно, как редактировать models.py - вслепую?
Локально никак не запустить?
нужно сделать миграции: создать и применить, а потом запустить сервер и посмотреть уже в админке что и как мы же меняем бд меняя models. но нужно об этом сообщить python, то есть сделать миграции
I think I was in need of something like this: http://django.readthedocs.io/en/latest/releases/1.7.html#standalone-scripts
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.
@tinivir - can you please review this - a00c6b2403ea3bce0f0d7042d6f82546d5a43cc4
Can close this issue after review.
aqmwd
:freq = models.CharField(max_length=64, null=False)